High Performance Python (2nd Edition) cover

High Performance Python (2nd Edition)

No rating yet
Word count
486K words
Chapters
13
ISBN
9787115599476

Tags

Summary

This book is the Chinese translation of the second edition of O'Reilly's classic High Performance Python, written for Python 3, with 12 chapters in total. It does not teach introductory Python syntax; rather, it teaches you how to scientifically make your code "run fast" after it already "runs": first use profiling methods to find the real bottlenecks, then apply targeted remedies for three types of problems—CPU-bound, I/O-bound, and memory-bound. The book begins with computer architecture, explaining how computing units, storage hierarchies, and communication overhead determine the actual cost of Python code, then introduces profiling tools such as cProfile, SnakeViz, line_profiler, memory_profiler, PySpy, and bytecode disassembly, using the Julia set computation as a running case to gradually speed things up. The middle section discusses choosing between lists and tuples, dictionaries and sets, iterators and generators, NumPy matrix and vectorized computation, and using Cython to compile critical code into C extensions. The second half turns to concurrency and distribution: asyncio and asynchronous I/O, the multiprocessing module, clusters and job queues (including Docker deployment ideas), and reducing memory usage through means such as __slots__, chunking, array, and NumPy. The final chapter summarizes practical experience and lessons. This edition updates the toolchain and cases in the Python 3 ecosystem and is suitable as a systematic performance-tuning reference for readers with some foundation.

Editor's review

很多讲 Python 优化的书读起来像一堆技巧清单:换这个库、加那个装饰器,却说不清为什么快。这本书的价值恰恰在于相反的路子——它把“先度量、再优化”当成纪律来写。开篇先把计算单元、缓存层级、总线通信讲透,让你明白 Python 的每一点开销从哪里来;接着用一整章教你把剖析工具用到位,cProfile、line_profiler、memory_profiler、PySpy 各有分工,还提醒你在优化期间用单元测试守住正确性底线。 作者的写法很务实:同一个朱利亚集合例子,从纯 Python 一路加速到 NumPy 向量化、Cython 编译、多进程和集群,读者能清楚看到每一步提速的收益与代价,而不是记住一个结论。第2版更新到 Python 3 时代的工具链,异步 I/O、multiprocessing、Docker 部署等内容对今天的工程实践更有参考价值。它不承诺读完就变高手,但能帮你改掉“凭感觉优化”的习惯——这大概是性能类图书能给的最实在的东西。

Who it's for

A good fit for

  • 已掌握 Python 基础语法,想系统学习性能调优方法的中级开发者
  • 从事数据分析、科学计算、机器学习,苦于 Python 脚本运行太慢的从业者
  • 需要处理大规模数据或高并发 I/O 的后端与数据工程师
  • 希望理解 CPython 内部机制、对字节码与内存模型感兴趣的程序员

Not a good fit for

  • 完全零基础、还没写过完整 Python 程序的初学者
  • 只想找几条“一行代码提速”速成技巧、不愿动手剖析的人
  • 主要使用 PyPy 或 C 扩展生态、对 CPython 调优需求极低的读者

Reading tips

  1. 第1、2章务必细读并动手跑剖析示例,掌握“先测量后优化”的流程,这是全书的地基
  2. 按问题类型跳跃阅读:CPU 密集型重点看第3至7章,I/O 与并发看第8至10章,内存吃紧看第11章
  3. 把书中的朱利亚集合案例自己实现一遍,每读完一章就复现对应提速手段并记录耗时对比
  4. 在自己的项目中挑一个真实慢函数做练习,优化前后都留存剖析报告,避免过早优化

About the publisher

Posts & Telecom Press China · Founded in 1953

Posts & Telecom Press is known for its strengths in science and technology, computing, and information and communications titles, and has extended its publishing into photography, art, software learning, and inspirational growth, giving it a fairly diverse range. Its catalog includes computing and communications works such as Refactoring and Patterns (Revised Edition) and Huawei Switch Learning Guide (Second Edition), alongside bilingual collector's editions such as The Little Prince (Chinese-English-French illustrated collector's edition), The Great Gatsby, and Three Days to See. Beijing Ten Years: 1980-1990 and Ghost Blade: WLOP Personal Illustration Collection reflect its presence in photography and art. Its authors include Antoine de Saint-Exupéry, Helen Keller, Joshua Kerievsky, Wang Da, Zhang Zhaozeng, and WLOP.

Contents

  1. Preface
  2. Chapter 1 Understanding High-Performance Python
  3. Chapter 2 Finding Bottlenecks Through Profiling
  4. Chapter 3 Lists and Tuples
  5. Chapter 4 Dictionaries and Sets
  6. Chapter 5 Iterators and Generators
  7. Chapter 6 Matrix and Vector Computation
  8. Chapter 7 Compiling to C
  9. Chapter 8 Asynchronous I/O
  10. Chapter 9 The multiprocessing Module
  11. Chapter 10 Clusters and Job Queues
  12. Chapter 11 Reducing Memory Usage
  13. Chapter 12 Practical Lessons Learned

FAQ

这本书适合零基础学 Python 的人吗?
不太适合。本书假设你已能熟练编写 Python 程序,重点在于如何定位性能瓶颈并选择优化手段,几乎不讲解语法、数据结构等入门内容。完全零基础建议先系统学完 Python 基础,再回头读这本。
第一版和第二版有什么区别?
第一版2017年出版、由胡世杰与徐旭彬翻译;第2版2023年由张海龙翻译,按原书第2版更新至 Python 3 生态,工具链与案例都有调整,章节结构仍为12章。新读者建议直接从第2版入手。
书里的代码需要什么环境?
书中基于 Python 3,示例涉及 NumPy、Cython、line_profiler、memory_profiler、PySpy、multiprocessing 与 asyncio 等,需要自行安装相应第三方包;第7章编译 C 扩展还需要本机具备 C 编译器。建议用独立虚拟环境跟着实验。
读完能立刻让我的程序快十倍吗?
不能保证。本书给的是方法论和工具箱:剖析找瓶颈、按瓶颈类型选方案。实际收益取决于你的代码瓶颈在哪里,可能来自算法、数据结构、I/O 或内存布局。真正提速需要你把方法用到自己的代码上反复验证。

Comments

Find this book

This site is an index: you start “Search all sources” in your browser, and the entries it lists all point to third-party sites; please judge their availability yourself.

Top rated in the same category