Python指令碼效能剖析

來源:互聯網
上載者:User

標籤:creat   ble   內部使用   file   hot   standard   current   local   compile   

###################
#Python指令碼效能剖析
###################

cProfile/profile/hotshot用於統計Python指令碼各部分運行頻率和耗費時間等統計資訊。pstats可用于格式化這些資訊

cProfile,屬C擴充。開銷較小,適合剖析長時間執行的Python程式,推薦使用此模組
profile。純Python模組,存在明顯開銷,但想對其擴充的話相對照較easy
hotshot,實驗性的C模組。主要關注開銷最小化,現已不再被維護將來可能從Python移除
profile和cProfile介面同樣。cProfile相對較新可能在某些平台上不可用

以cProfile為例來說明Python指令碼效能剖析方法
*以命令列方式使用:
$ python -m cProfile [-o output_file] [-s sort_order] myscript.py
比如:
$ python -m cProfile -o myscript.out myscript.py
之後使用pstats對結果進行格式化:
$ python -c "import pstats; p=pstats.Stats(‘myscript.out‘); p.print_stats()"
能夠在格式化時指定排序欄位:
$ python -c "import pstats; p=pstats.Stats(‘myscript.out‘); p.sort_stats(‘time‘).print_stats()"


*直接在指令碼內部使用:
import cProfile
import re
cProfile.run(‘re.compile("foo|bar")‘, ‘restats‘)

import pstats
p = pstats.Stats(‘restats‘)
#strip_dirs()移除模組名之前的路徑資訊,sort_stats(-1)按標準名(module/line/name)排序,print_stats列印統計資訊
p.strip_dirs().sort_stats(-1).print_stats()
#按time排序並顯示前10行
p.sort_stats(‘time‘).print_stats(10)
#按file排序僅僅顯示class init方法相關的統計資訊
p.sort_stats(‘file‘).print_stats(‘__init__‘)
#先按time排序再按cum排序,僅僅輸出50%。然後僅列出包括init的部分
p.sort_stats(‘time‘, ‘cum‘).print_stats(.5, ‘init‘)
#若想知道誰調用了上述函數能夠使用
p.print_callers(.5, ‘init‘)

*cProfile模組說明
函數:
cProfile.run(command, filename=None, sort=-1)
cProfile.runctx(command, globals, locals, filename=None)?
類:
cProfile.Profile(timer=None, timeunit=0.0, subcalls=True, builtins=True)
cProfile.Profile類下的方法:
enable()
Start collecting profiling data.

disable()
Stop collecting profiling data.

create_stats()
Stop collecting profiling data and record the results internally as the current profile.

print_stats(sort=-1)
Create a Stats object based on the current profile and print the results to stdout.

dump_stats(filename)
Write the results of the current profile to filename.

run(cmd)
Profile the cmd via exec().

runctx(cmd, globals, locals)
Profile the cmd via exec() with the specified global and local environment.

runcall(func, *args, **kwargs)
Profile func(*args, **kwargs)

*Stats類(pstats.Stats)說明
strip_dirs()    用以除去檔案名稱前的路徑資訊。
add(filename,[…]) 把profile的輸出檔案增加Stats執行個體中統計
dump_stats(filename) 把Stats的統計結果儲存到檔案
sort_stats(key,[…]) 最重要的一個函數,用以排序profile的輸出
reverse_order() 把Stats執行個體裡的資料反序重排
print_stats([restriction,…]) 把Stats報表輸出到stdout
print_callers([restriction,…]) 輸出調用了指定的函數的函數的相關資訊
print_callees([restriction,…]) 輸出指定的函數調用過的函數的相關資訊

sort_stats支援下面參數:

參數 含義

‘calls‘ call count
‘cumulative‘ cumulative time
‘cumtime‘ cumulative time
‘file‘ file name
‘filename‘ file name
‘module‘ file name
‘ncalls‘ call count
‘pcalls‘ primitive call count
‘line‘ line number
‘name‘ function name
‘nfl‘ name/file/line
‘stdname‘ standard name
‘time‘ internal time
‘tottime‘ internal time


*一個比較典型的輸出結果:

   197 function calls (192 primitive calls) in 0.002 seconds
Ordered by: standard name
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.001    0.001 <string>:1(<module>)
     1    0.000    0.000    0.001    0.001 re.py:212(compile)
     1    0.000    0.000    0.001    0.001 re.py:268(_compile)
     1    0.000    0.000    0.000    0.000 sre_compile.py:172(_compile_charset)
     1    0.000    0.000    0.000    0.000 sre_compile.py:201(_optimize_charset)
     4    0.000    0.000    0.000    0.000 sre_compile.py:25(_identityfunction)
   3/1    0.000    0.000    0.000    0.000 sre_compile.py:33(_compile)

輸出結果說明:

共同擁有197次函數調用,原始調用為192次,原始調用說明不包括遞迴調用。


以standard name進行排序。

3/1表示發生了遞迴調用,1為原始調用次數,3為遞迴調用次數
ncalls 函數的被調用次數
tottime 函數總計執行時間。除去函數中調用的函數執行時間
percall 函數執行一次的平均時間,等於tottime/ncalls
cumtime 函數總計執行時間。含調用的函數執行時間
percall 函數執行一次的平均時間。等於cumtime/ncalls
filename:lineno(function) 函數所在的檔案名稱,函數的行號。函數名


參考:
https://docs.python.org/2/library/profile.html

Python指令碼效能剖析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.