Skip to Content
Python入门指南, 3rd Edition
book

Python入门指南, 3rd Edition

by Bill Lubanovic
September 2025
Intermediate to advanced
660 pages
7h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Python入门指南, 3rd Edition

第 27 章 性能 性能

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

有没有想过,如果闪电不走之字形路线,速度会是多少?

史蒂文-赖特

Python 通常已经够快了--直到它不够快。在许多情况下,您可以通过使用更好的算法或数据结构来提高速度。 诀窍在于知道该从哪里下手。 即使是经验丰富的程序员也经常会出人意料地猜错。 您需要像细心的裁缝一样,在裁剪之前先测量一下。 这让我们首先想到定时器

在此之后,我们将介绍许多加快代码速度的技术:算法、数据结构以及用更快的语言(如 C 和 Rust)编写的辅助工具。

我们甚至还将了解一种有趣的新语言,它的目标是成为 Python++ - 与 Python 一样友好,比 C 语言更快(!),并且适用于人工智能开发和普通编程。

计算机硬件

第 2 章中,我 对计算机的内部结构进行了简化描述。现在让我们来了解更多细节。

计算机内部有各种电路板,上面装有多个电子元件,通过电线连接,有的可见,有的很小。 计算核心是......内核,即中央处理器(CPU)。 以前只有一个,现在有很多。每个内核都有以下功能:

寄存器

保存输入数据并输出结果

算术逻辑单元 (ALU)

处理指令

控制单元

获取指令和数据

时钟

同步一切

从每个内核向外扩散的是这些元素:

  • 多个内存缓存

  • 随机存取存储器 (RAM)

  • 固态硬盘 (SSD) 或闪存

  • 一个或多个磁性硬盘

前两类是易失性的:绊倒电源线就会丢失其中的数据。 后两类在断电后仍能保留数据。

表 27-1是《Coding Gopher》YouTube 频道 一集中具有代表性的现代数据表的修改版:

表 27-1. 数据位置、速度和大小
函数名称 CPU 周期 时间(毫秒) 大小(字节)

CPU 寄存器

1

0.3

~100

L1 高速缓存

1-4

1.1

64K

二级缓存

10-20

3.3

256K

L3 高速缓存

20-50

12.8

8M

内存

100-200

62.9

32G

固态硬盘

3,000

~1,000

200G-1T

磁盘

10,000

~3,000

2-5T

越往下,存储空间越大,成本越低,速度越慢。 为了提高性能,我们希望尽可能多地在最上面几行进行操作。 程序和数据的长期存储在最下面几行。

操作系统负责处理各行之间的大量移动,如 虚拟内存(从固态硬盘或硬盘分页到内存)。我们都看到过,当计算机繁忙起来,开始在磁盘和内存之间交换数据时,性能会急剧下降,因为磁盘的速度要慢数千倍。

虽然计算机硬件和操作系统会努力让字节愉快地流动,但我们可以通过以下方法在软件层面提高性能:

数据

连续数据可以一起缓存和访问。独立创建的数据可能分散在 RAM 中,无法利用快速缓存。 阵列可能比单独项目列表更快。

并行性

过去,CPU 一次只能处理一个操作,而现在通过支持单指令、多数据(SIMD),CPU 可以同时处理多个操作。

并发

正如你在第 21 章中所学到的,使用并发功能尤其可以避免不涉及 CPU 的磁盘和网络 I/O 的 "繁忙等待"。

让我们从如何在 Python 中测量定时开始,然后访问许多提高性能的方法。

测量定时

您已经在 看到time 模块中的time() 函数 以浮点数秒的形式返回当前的纪元时间。快速计时的方法是获取当前时间,执行任务,获取新时间,然后用新时间减去原来的时间。 让我们把它写出来,如例 27-1 ...

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

生成式人工智能设计模式

生成式人工智能设计模式

Valliappa Lakshmanan, Hannes Hapke

Publisher Resources

ISBN: 9798341668898