High Performance Python book cover showing abstract layered geometric forms suggesting speed and computational throughput

Published

2025

Python ✨ New

High Performance Python

Practical Performant Programming for Humans

Profile, optimize, and scale Python code so your programs run faster without sacrificing readability or correctness.

High Performance Python gives you a systematic toolkit for finding and fixing bottlenecks in real Python programs. Starting with profiling to locate what actually matters, the book moves through memory management, efficient data structures, compiled extensions, concurrency, and parallel computing. Written by practitioners for practitioners, it gives you the mental models and hands-on techniques to make Python fast enough for the most demanding workloads.

About this book

Python is expressive and approachable, but production workloads have a way of exposing its limits. CPU-bound loops stall, memory balloons, I/O queues back up, and suddenly the language everyone loves becomes the thing your infrastructure team is asking you to replace. This book shows you how to fix that without throwing away your codebase.

Micha Gorelick and Ian Ozsvald walk you through a disciplined, evidence-driven approach to optimization. You start by learning to measure before you change anything. Profiling tools β€” cProfile, line_profiler, memory_profiler, perf β€” tell you where time and memory actually go, not where you assume they go. That distinction matters: most optimization effort spent without profiling is spent on the wrong code.

Once you know where the bottleneck is, the book equips you to address it precisely. You will learn why Python lists, sets, and dicts behave the way they do at the memory level, and how to choose the right structure for the job. You will learn when NumPy vectorization eliminates Python loops entirely, and when Cython or Numba can compile the remainder to near-C speed. You will understand the GIL β€” what it actually prevents, what it does not, and how to work around it with threading, multiprocessing, and async I/O patterns that match the problem you are solving.

The later chapters address large-scale and distributed workloads: efficient use of multiple cores, cluster-level parallelism, and patterns for keeping memory overhead predictable as data volumes grow. Throughout, the authors treat correctness and maintainability as non-negotiable constraints, not trade-offs.

  • Profile Python programs with CPU, wall-clock, and memory profilers to isolate true bottlenecks
  • Understand Python's memory model and object overhead to write allocation-friendly code
  • Accelerate numerical workloads with NumPy and avoid common vectorization pitfalls
  • Compile hot paths with Cython and Numba for near-native throughput
  • Apply threading, multiprocessing, and asyncio to the right classes of problem
  • Scale computation across multiple cores and machines without introducing subtle concurrency bugs

This is the updated 2025 edition, revised to cover current Python versions and modern tooling. Whether you are optimizing a data pipeline, a web service under load, or a scientific simulation, the techniques here translate directly to production code you ship.

🎯 What you'll learn

  • Profile CPU and memory usage to identify exactly which lines of code deserve optimization effort
  • Understand the CPython memory model well enough to predict and reduce object overhead
  • Vectorize numerical loops using NumPy to eliminate Python interpreter overhead
  • Compile performance-critical code paths with Cython and Numba
  • Choose correctly between threading, multiprocessing, and asyncio for a given I/O or CPU workload
  • Avoid common concurrency pitfalls including GIL contention and shared-state race conditions
  • Scale Python workloads across multiple cores and distributed systems
  • Measure the impact of every optimization to confirm it actually helps

πŸ‘€ Who is this book for?

  • Intermediate Python developers who have working code and need it to run significantly faster
  • Data engineers and scientists whose pipelines are hitting memory or throughput limits
  • Backend engineers profiling Python services that struggle under production load
  • Researchers and analysts moving from exploratory notebooks to performance-sensitive applications
  • Platform engineers evaluating whether Python can meet latency or throughput requirements before committing to a rewrite

Table of contents

  1. 01

    Understanding Performant Python

    Establishes a mental model for why Python has the performance characteristics it does, covering the interpreter loop, the GIL, and the cost of dynamic typing. Sets up the measurement-first philosophy that runs through the rest of the book.

  2. 02

    Profiling to Find Bottlenecks

    Walks through the main Python profiling tools β€” cProfile, line_profiler, and perf β€” with worked examples showing how to interpret their output and avoid common measurement mistakes.

  3. 03

    Lists, Tuples, and Memory Layouts

    Examines how Python allocates and stores sequence types under the hood, explaining when to prefer each and how to reduce per-object overhead through smarter data structure choices.

  4. 04

    Dictionaries and Sets

    Explains the hash-table internals behind dict and set, covering memory cost, lookup performance, and the practical cases where choosing one over the other makes a measurable difference.

  5. 05

    Iterators and Generators

    Shows how lazy evaluation with generators and itertools reduces peak memory use in data-processing pipelines, with before-and-after benchmarks on real workloads.

  6. 06

    NumPy and Vectorization

    Covers NumPy array internals, broadcasting rules, and the patterns that eliminate Python-level loops, along with the common mistakes that silently negate vectorization gains.

  7. 07

    Compiling with Cython and Numba

    Teaches you to identify code that benefits from compilation, then apply Cython type annotations and Numba JIT decorators to achieve near-C throughput on the hot paths that remain.

  8. 08

    Concurrency, Parallelism, and the GIL

    Distinguishes I/O-bound from CPU-bound concurrency, explains precisely what the GIL blocks, and shows idiomatic patterns using threading, multiprocessing, and asyncio for each problem class.

  9. 09

    Multiprocessing and Distributed Computing

    Covers the multiprocessing module, process pools, shared memory, and an introduction to cluster-level tools, with guidance on avoiding serialization overhead that can erase parallelism gains.

  10. 10

    Practical Optimization Workflow

    Synthesizes the book's techniques into a repeatable workflow: profile, hypothesize, change one thing, measure again. Includes a set of common anti-patterns and how to recognize them in your own code.

Frequently asked questions

What Python version does this edition target?

The 2025 edition covers Python 3.11 and 3.12, including changes to the GIL and the new free-threaded build. Earlier Python 3 syntax is used where it aids clarity, and all code samples are compatible with Python 3.10 and above.

Do I need a background in systems programming or C to benefit from this book?

No C knowledge is required for most chapters. The Cython and Numba chapters introduce just enough C-adjacent syntax to write annotated extensions, and each concept is explained from scratch.

Is this book suitable for beginners?

The book assumes you are already comfortable writing Python programs β€” functions, classes, file I/O, and basic data structures. If you are still learning the language fundamentals, you will get more from this book after a few months of practical Python experience.

Does the book cover async Python and asyncio?

Yes. There is dedicated coverage of asyncio patterns, when async I/O helps versus when it does not, and how to combine it with other concurrency approaches without introducing hard-to-debug race conditions.

Are code samples available separately?

O'Reilly books in this format typically include code examples you can use in your own programs. Check the publisher's page for any companion repository linked from the book's official listing.

How is this edition different from the previous one?

The 2025 edition is fully revised for Python 3.11 and 3.12, updates profiling tooling recommendations, expands the Numba and asyncio coverage, and adds new material on the experimental free-threaded CPython build.

You might also like

πŸ“¬ Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.