Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorMicha Gorelick, Ian Ozsvald

Your Python code may run correctly, but you need it to run faster. Updated for Python 3, this expanded edition shows you how to locate performance bottlenecks and significantly speed up your code in high-data-volume programs. By exploring the fundamental theory behind design choices, High Performance Python helps you gain a deeper understanding of Python’s implementation. How do you take advantage of multicore architectures or clusters? Or build a system that scales up and down without losing reliability? Experienced Python programmers will learn concrete solutions to many issues, along with war stories from companies that use high-performance Python for social media analytics, productionized machine learning, and more. • Get a better grasp of NumPy, Cython, and profilers • Learn how Python abstracts the underlying computer architecture • Use profiling to find bottlenecks in CPU time and memory usage • Write efficient programs by choosing appropriate data structures • Speed up matrix and vector computations • Use tools to compile Python down to machine code • Manage multiple I/O and computational operations concurrently • Convert multiprocessing code to run on local or remote clusters • Deploy code faster using tools like Docker

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, profiling-first guide for experienced Python programmers who need to make their code run faster and scale further, covering everything from CPU and memory bottlenecks to NumPy, Cython, concurrency, and cluster computing. 【Book Arc】 - **Opening (~0%–9%)**: Sets the stage with the book's mission—making Python faster for high-data-volume work—and establishes Python 3 (64-bit) as the baseline. It outlines the major topics: profiling, data structures, NumPy, compilation, concurrency, multiprocessing, clusters, and memory reduction. - **Early (~9%–25%)**: Introduces the fundamental computer architecture (CPU, RAM, caches, SIMD, hyperthreading) and explains why moving data is expensive. It also covers "highly performant programming" habits, including using Notebooks effectively, asserting data, and structuring code for later optimization. - **Early (~25%–34%)**: Dives into profiling with a real Julia set example. It demonstrates how to use cProfile and line_profiler to find bottlenecks, showing that even simple operations like `n += 1` carry Python's dynamic lookup overhead. - **Middle (~34%–47%)**: Continues profiling with memory_profiler and discusses bytecode efficiency (e.g., `sum(range(n))` vs. a manual loop). It then shifts to data structures, explaining how lists allocate memory overallocatively and why sets beat lists for uniqueness checks (O(1) vs. O(n^2)). - **Late (~47%–100%)**: Covers advanced topics: efficient text storage with hashing and sparse matrices, probabilistic data structures (Bloom filters, LogLog counters), and "lessons from the field" on deploying feature engineering pipelines. The book closes with cluster design and avoiding operational pain. 【Key Takeaways】 - **Profiling is the non-negotiable first step** (Early): Use cProfile for function-level timing and line_profiler for line-by-line breakdowns; memory_profiler tracks RAM. Without this, optimization is guesswork—the book's Julia set example shows how profiling reveals that a `while` loop condition eats 38% of time. - **Python's dynamic machinery is the hidden tax** (Early): Every operation like `n += 1` triggers method lookups and type checks. This explains why compiled approaches (Cython, Numba) yield massive wins—they specialize types and bypass this overhead. - **Lists overallocate memory deliberately** (Middle): Python lists grow by a formula (e.g., `M = (N >> 3) + 6` for larger N), trading memory for amortized O(1) appends. Understanding this helps you predict memory usage and choose between lists and arrays. - **Sets and dictionaries beat lists for membership tests** (Middle): A list-based uniqueness check is O(n^2) because each lookup scans a growing list; a set-based check is O(n) overall because `add` is O(1). Choose the right data structure before optimizing loops. - **Vectorization is a CPU-level win** (Early): The CPU can run one instruction on multiple data (SIMD). By structuring code to operate on arrays (e.g., NumPy), you minimize data transfers between RAM and cache, which is often the real bottleneck. - **Compilation and JIT are the escape hatches** (Late): Tools like Cython compile Python to machine code, and JIT compilers specialize types at runtime. These are guided by profiling results and can turn a 49-second function into something near-instant. - **Probabilistic data structures save RAM at scale** (Late): Bloom filters and LogLog counters approximate membership and cardinality with tiny memory footprints, useful for "11 million tokens" problems where exact counts are infeasible. 【Reading Tips】 - **Skim the architecture chapter (Early)**: You don't need to memorize CPU details, but grasp the "heavy data" concept—moving data is slow. This mental model underpins every later optimization. - **Deep-read the profiling chapters (Early–Middle)**: Follow the Julia set example closely. Run the code yourself if possible; the difference between cProfile, line_profiler, and memory_profiler is best learned by doing. - **Skip the Notebook etiquette if you're not a data scientist**: The assert-and-refactor advice is useful, but the core value is in the profiling and data structure chapters. - **Treat the cluster and field chapters as reference**: Skim them to know what's possible (e.g., converting multiprocessing to clusters, using Docker), but don't memorize—you'll revisit when you hit that scale. - **Take away the "profile first" mantra**: The book's biggest lesson is that intuition about performance is often wrong. Always measure before optimizing, and re-measure after. 【Coverage Limits】 This guide synthesizes the opening through middle sections (architecture, profiling, data structures) and outlines the later topics (NumPy, Cython, concurrency, clusters, memory) based on the table of contents and excerpts. Detailed code examples for advanced chapters (e.g., cluster computing, feature engineering) are not covered in depth here.
Excerpt 1
318 Two Clustering Solutions 319 Using IPython Parallel to Support Research 319 Parallel Pandas with Dask 322 NSQ for Robust Production Clustering 326 Queues...
View in text
Excerpt 2
sqrt_number, we need to send the value of number to the CPU. Ideally, we could send the value once; it would get stored inside the CPU’s L1/L2 cache, and the...
View in text
Excerpt 3
ter overhead, but you get correspondingly more information. Sometimes the additional information can lead to surprising insights into your code. cProfile is...
View in text
Excerpt 4
apter 3: Lists and Tuples ("John Murphey", "202-555-5555"), ("Albert Rutherford", "647-555-5555"), ("Guido van Rossum", "301-555-5555"), ] print("Number of u...
View in text
Excerpt 5
hat are anomalous without having to load the entire dataset. Only enough data is read to generate the first five anomalies. Additionally, the anomaly_generat...
View in text
Excerpt 6
e was taken to make sure that the code was fully documented and tested to help not only us but also other people on the team. Sometimes, however, your numeri...
View in text
Excerpt 7
7-9 looks a little like the original implementation, except that we have added memoryview annotations. The function’s second argument is dou ble complex[:] z...
View in text
Excerpt 8
he ctypes module is great if you already have a good under‐ standing of C and want to be able to tune every aspect of the interface. It ha
View in text
Tags
AI categories
PythonProgramming LanguageBackend
ISBN: 1492055026
Publisher: O'Reilly Media
Publish Year: 2020
Language: English
Pages: 469
File Format: PDF
File Size: 10.6 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…