Skip to Content
《高性能 Python》第二版
book

《高性能 Python》第二版

by Micha Gorelick, Ian Ozsvald
May 2025
Intermediate to advanced
468 pages
6h 20m
Chinese
O'Reilly Media, Inc.
Content preview from 《高性能 Python》第二版

第 2 章 剖析查找瓶颈

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

剖析可以让我们找到瓶颈,从而以最少的工作量获得最大的实际性能提升。虽然我们希望能以最少的工作量获得巨大的速度提升并减少资源占用,但实际上,您的目标是让代码运行得 "足够快 "和 "足够精简",以满足您的需求。剖析可以让您以最少的投入做出最务实的决定。

任何可测量的资源都可以进行剖析(不仅仅是 CPU!)。本章将介绍 CPU 时间和内存使用情况。你也可以应用类似的技术来测量网络带宽和磁盘 I/O。

如果程序运行速度过慢或内存占用过多,你就需要修复代码中需要负责的部分。当然,您可以跳过剖析,修复您认为可能存在问题的部分,但要小心,因为您最终往往会 "修复 "错误的部分。比起凭直觉,更明智的做法是先进行剖析,提出假设,然后再修改代码结构。

有时,偷懒是件好事。首先进行剖析,可以快速找出需要解决的瓶颈问题,然后只需解决足够的问题就能达到所需的性能。如果避开剖析而直接进行优化,从长远来看很可能会增加工作量。始终以剖析结果为导向。

高效剖析

剖析的首要目的是测试一个具有代表性的系统,以确定哪些地方速度较慢(或使用了过多内存,或导致过多磁盘 I/O 或网络 I/O)。剖析通常会增加开销(通常会造成 10 倍到 100 倍的速度减慢),但您仍然希望您的代码能在尽可能类似真实世界的情况下使用。提取测试用例并隔离需要测试的系统部分。最好已经编写了一套独立的模块。

本章首先介绍的基本技术包括 IPython 中的%timeit 魔术、time.time() 和定时装饰器。您可以使用这些技术来理解语句和函数的行为。

然后,我们将介绍 cProfile"使用 cProfile 模块"),告诉您如何使用这一内置工具来了解代码中哪些函数的运行时间最长。这将为您提供一个高层次的问题视图,以便您将注意力集中到关键函数上。

接下来,我们将学习line_profiler"使用 line_profiler 进行逐行测量"),它将对所选函数进行逐行剖析。结果将包括每一行被调用的次数和每一行所花费时间的百分比。这正是您需要的信息,可以帮助您了解哪些功能运行缓慢以及原因。

有了line_profiler 的结果,您就掌握了继续使用编译器所需的信息(第 7 章)。

第 6 章中,您将学习如何使用 perf stat 来了解 CPU 最终执行的指令数以及 CPU 缓存的利用效率。这样就可以对矩阵运算进行高级调整。学完本章后,你应该看看例 6-8。

line_profiler 之后,如果你正在处理长期运行的系统,那么你会对py-spy 感兴趣,因为它可以窥视已经运行的 Python 进程。

为了帮助您了解内存使用率高的原因,我们将向您展示 memory_profiler"使用 memory_profiler 诊断内存使用情况")。它对于在带标签的图表上跟踪一段时间内的内存使用情况特别有用,这样你就可以向同事解释为什么某些函数的内存使用量比预期的要高。

警告

无论您采用哪种方法对代码进行剖析,都必须记住在代码中包含足够的单元测试覆盖率。单元测试可以帮助您避免愚蠢的错误,并保持结果的可重复性。避免单元测试,后果自负。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

《学习 Python》第 5 版

《学习 Python》第 5 版

Mark Lutz
ppk on JavaScript

ppk on JavaScript

Peter-Paul Koch

Publisher Resources

ISBN: 9798341657946