更多深度文章,请关注:https://yq.aliyun.com/cloud
本文假定你已经十分熟悉 Python。
众所周知,Python 是一种解释性的语言,执行速度相比 C、C++ 等语言十分缓慢;因此我们需要在其它地方上下功夫来提高代码的执行速度。
首先需要对代码进行分析。
代码分析
傻乎乎地一遍又一遍地检查代码并不会对分析代码的执行时间有多大帮助,你需要借助一些工具。
先看下面这段程序:
瓶颈在磁盘存取,很显然易见是不是?我们走着瞧。
- """Sorting a large, randomly generated string and writing it to disk"""
- import random
- def write_sorted_letters(nb_letters=10**7):
- random_string = ''
- for i in range(nb_letters):
- random_string += random.choice('abcdefghijklmnopqrstuvwxyz')
- sorted_string = sorted(random_string)
- with open("sorted_text.txt", "w") as sorted_text:
- for character in sorted_string:
- sorted_text.write(character)
- write_sorted_letters()
调优器(profiler)能够精确地告诉我们程序在执行时发生了什么。它能够自动计时并计数程序中的每一行代码,从而节省大量时间,是优化代码的第一选择。
全代码分析
所有合格的 IDE 都集成有一个调优器,点一下就可以了;如果是在命令行中进行调用,代码如下:
- python -m cProfile -s tottime your_program.py
结果如下:
结果按总时间排序 (-s tottime),靠前的更应该被优化。本例中,random 模组中的 choice 函数花费了总时间的将近 1/3,现在你知道瓶颈在哪里了吧。
- 40000054
- function calls in 11.362 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename: lineno(function) 10000000 4.137 0.000 5.166 0.000 random.py: 273(choice) 1 3.442 3.442 11.337 11.337 sort.py: 5(write_sorted_letters) 1 1.649 1.649 1.649 1.649 {
- sorted
- }
- 10000000 0.960 0.000 0.960 0.000 {
- method 'write'of 'file'objects
- }
- 10000000 0.547 0.000 0.547 0.000 {
- method 'random'of '_random.Random'objects
- }
- 10000000 0.482 0.000 0.482 0.000 {
- len
- }
- 1 0.121 0.121 0.121 0.121 {
- range
- }
- 1 0.021 0.021 11.362 11.362 sort.py: 1( < module > )...
迫不及待去做优化了?别急,代码分析有好几种方法。
块分析
你可能已经注意到,之前我们是对整个程序段进行分析的。如果你只对某一部分代码感兴趣,只需要在这部分代码的前后加上下面这两段代码即可:
- import cProfile
- cp = cProfile.Profile()
- cp.enable()
- and
- cp.disable()
- cp.print_stats()
结果与全代码分析的类似,但是只包含你感兴趣的部分。但是一般来说,你不应该直接使用块分析,在这之前请务必先做因此全代码分析。
有关 cProfile 还有 Profile 的更多信息,请点击。
行分析
比块分析更精确地是行分析。进行行分析需要额外安装 line_profiler:
pip install line_profiler
安装成功后,修改代码,在每一行你想分析的代码前增加 @profile,如下所示:
- @profile
- def write_sorted_letters(nb_letters=10**7):
- ...
最后在命令行中输入如下代码:
- kernprof -l -v your_program.py
- · -l 逐行分析
- · -v 立即查看结果
结果如下所示:
- Total time: 21.4412 s
- File: ./sort.py
- Function: write_sorted_letters at line 5
- Line # Hits Time Per Hit % Time Line Contents
- ================================================================
- 5 @profile
- 6 def write_sorted_letters(nb_letters=10**7):
- 7 1 1 1.0 0.0 random_string = ''
- 8 10000001 3230206 0.3 15.1 for _ in range(nb_letters):
- 9 10000000 9352815 0.9 43.6 random_string += random.choice('abcdefghijklmnopqrstuvwxyz')
- 10 1 1647254 1647254.0 7.7 sorted_string = sorted(random_string)
- 11
- 12 1 1334 1334.0 0.0 with open("sorted_text.txt", "w") as sorted_text:
- 13 10000001 2899712 0.3 13.5 for character in sorted_string:
- 14 10000000 4309926 0.4 20.1 sorted_text.write(character)
注意,代码执行的速度变慢了,从 11 秒上升到了 21 秒。但是瑕不掩瑜,我们知道了是哪一行拖了整段代码的后腿。
实时不间断网页应用该如何分析代码?
我们先来看一下需要的 Profiling module。
安装后通过如下命令运行:profiling your_program.py。不要忘了删除在行分析中使用的装饰器(@profile)。
结果如下所示:
结果是交互式的,你可以使用方向键轻松浏览或者折叠 / 打开每一行。
如果是需要长时间运行的程序(譬如网页服务器),也有响应的分析代码,命令类似于:profiling live-profile your_server_program.py。一旦开始运行,你可以在程序运行时与之交互,并观察程序的性能。
分析方法
优化
想知道你是否在循环中浪费了大量时间?现在我们知道程序在哪些地方花费了大量 CPU 时间,我们可以针对性的进行优化。
注意
只有在必要的时候和必要的地方才进行优化,因为优化后的代码通常比优化前更加难以理解和维护。
简单而言,优化是拿可维护性换取性能。
Numpy
看起来 random.choice 函数拖了后腿,就让我们使用著名的 numpy 库中的类似函数来代替它。新代码如下:
- """Sorting a large, randomly generated string and writing it to disk"""
- from numpy import random
- def write_sorted_letters(nb_letters=10**7):
- letters = tuple('abcdefghijklmnopqrstuvwxyz')
- random_letters = random.choice(letters, nb_letters)
- random_letters.sort()
- sorted_string = random_letters.tostring()
- with open("sorted_text.txt", "w") as sorted_text:
- for character in sorted_string:
- sorted_text.write(character)
- write_sorted_letters()
Numpy 包含有许多强大且速度块的数学函数,安装命令为:pip install numpy。
对优化后的代码进行性能分析,结果如下:
- 10011861
- function calls(10011740 primitive calls) in 3.357 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename: lineno(function) 10000000 1.272 0.000 1.272 0.000 {
- method 'write'of 'file'objects
- }
- 1 1.268 1.268 3.321 3.321 numpy_sort.py: 5(write_sorted_letters) 1 0.657 0.657 0.657 0.657 {
- method 'sort'of 'numpy.ndarray'objects
- }
- 1 0.120 0.120 0.120 0.120 {
- method 'choice'of 'mtrand.RandomState'objects
- }
- 4 0.009 0.002 0.047 0.012 __init__.py: 1( < module > ) 1 0.003 0.003 0.003 0.003 {
- method 'tostring'of 'numpy.ndarray'objects
- }...
新代码比之前的版本块了将近 4 倍(3.3 秒 vs11.362 秒)!现在轮到写操作拖后腿了,优化方法是舍弃如下代码
- with open("sorted_text.txt", "w") as sorted_text:
- for character in sorted_string:
- sorted_text.write(character)
代之以如下代码:
- with open("sorted_text.txt", "w") as sorted_text:
- sorted_text.write(sorted_string)
新代码一次写入整个字符串,而之前是逐个字符写入。
统计一下整段代码的时间,如下所示:
- time python your_program.py
- Which gives us:
- real 0m0.874s
- user 0m0.852s
- sys 0m0.280s
总时间从 11 秒减少到了不到 1 秒!是不是很棒?
其它优化技巧
记住电脑中的这些参数
- Latency Comparison Numbers--------------------------L1 cache reference 0.5 ns Branch mispredict 5 ns L2 cache reference 7 ns 14x L1 cache Mutex lock / unlock 25 ns Main memory reference 100 ns 20x L2 cache,
- 200x L1 cache Compress 1K bytes with Zippy 3,
- 000 ns 3 us Send 1K bytes over 1 Gbps network 10,
- 000 ns 10 us Read 4K randomly from SSD * 150,
- 000 ns 150 us~1GB / sec SSD Read 1 MB sequentially from memory 250,
- 000 ns 250 us Round trip within same datacenter 500,
- 000 ns 500 us Read 1 MB sequentially from SSD * 1,
- 000,
- 000 ns 1,
- 000 us 1 ms~1GB / sec SSD,
- 4X memory Disk seek 10,
- 000,
- 000 ns 10,
- 000 us 10 ms 20x datacenter roundtrip Read 1 MB sequentially from disk 20,
- 000,
- 000 ns 20,
- 000 us 20 ms 80x memory,
- 20X SSD Send packet CA - >Netherlands - >CA 150,
- 000,
- 000 ns 150,
- 000 us 150 ms
来源: https://yq.aliyun.com/articles/91140