使用cProfile等工具來提高python的執行速度
更多深度文章,請關注: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
結果如下:
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>)
...
結果按總時間排序(-s tottime),靠前的更應該被優化。本例中,random模組中的choice函數花費了總時間的將近1/3,現在你知道瓶頸在哪裏了吧。
迫不及待去做優化了?別急,代碼分析有好幾種方法。
塊分析
你可能已經注意到,之前我們是對整個程序段進行分析的。如果你隻對某一部分代碼感興趣,隻需要在這部分代碼的前後加上下麵這兩段代碼即可:
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
來自於Latency Numbers Every Programmer Should Know
其它資源
· Numpy
· Numba,通過JIT(just in time)甚至GPU的使用來加速代碼。
· Anaconda,一個集成環境,包含了Numpy、Numba以及其它許多針對數據科學還有數學計算的擴展包。
作者:Sylvain Josserand。
譯者注:原文提供的代碼在驗證時存在些許問題,可能是版本不一造成的。
本文由北郵@愛可可-愛生活老師推薦,阿裏雲雲棲社區組織翻譯。
文章原標題《Profiling and optimizing your Python code》,作者:Sylvain Josserand,譯者:楊輝,審閱:,附件為原文的pdf。
文章為簡譯,更為詳細的內容,請查看原文
最後更新:2017-05-28 18:01:25