Python性能分析工具Profile使用實例
這篇文章主要介紹了Python性能分析工具Profile使用實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
代碼優(yōu)化的前提是需要了解性能瓶頸在什么地方,程序運(yùn)行的主要時間是消耗在哪里,對于比較復(fù)雜的代碼可以借助一些工具來定位,python 內(nèi)置了豐富的性能分析工具,如 profile,cProfile 與 hotshot 等。其中 Profiler 是 python 自帶的一組程序,能夠描述程序運(yùn)行時候的性能,并提供各種統(tǒng)計幫助用戶定位程序的性能瓶頸。Python 標(biāo)準(zhǔn)模塊提供三種 profilers:cProfile,profile 以及 hotshot。
profile 的使用非常簡單,只需要在使用之前進(jìn)行 import 即可,也可以在命令行中使用。
使用Profile
測試示例:
import profile def a(): sum = 0 for i in range(1, 10001): sum += i return sum def b(): sum = 0 for i in range(1, 100): sum += a() return sum if __name__ == "__main__": profile.run("b()")
輸出結(jié)果:
<br data-filtered="filtered"> 104 function calls in 0.094 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.094 0.094 :0(exec) 1 0.000 0.000 0.000 0.000 :0(setprofile) 1 0.000 0.000 0.094 0.094 <string>:1(<module>) 1 0.000 0.000 0.094 0.094 profile:0(b()) 0 0.000 0.000 profile:0(profiler) 99 0.094 0.001 0.094 0.001 test.py:15(a) 1 0.000 0.000 0.094 0.094 test.py:21(b)
其中輸出每列的具體解釋如下:
●ncalls:表示函數(shù)調(diào)用的次數(shù);
●tottime:表示指定函數(shù)的總的運(yùn)行時間,除掉函數(shù)中調(diào)用子函數(shù)的運(yùn)行時間;
●percall:(第一個 percall)等于 tottime/ncalls;
●cumtime:表示該函數(shù)及其所有子函數(shù)的調(diào)用運(yùn)行的時間,即函數(shù)開始調(diào)用到返回的時間;
●percall:(第二個 percall)即函數(shù)運(yùn)行一次的平均時間,等于 cumtime/ncalls;
●filename:lineno(function):每個函數(shù)調(diào)用的具體信息;
如果需要將輸出以日志的形式保存,只需要在調(diào)用的時候加入另外一個參數(shù)。如 profile.run(“profileTest()”,”testprof”)。
命令行
如果我們不想在程序中調(diào)用profile庫使用,可以在命令行使用命令。
import os def a(): sum = 0 for i in range(1, 10001): sum += i return sum def b(): sum = 0 for i in range(1, 100): sum += a() return sum print b()
運(yùn)行命令查看性能分析結(jié)果
python -m cProfile test.py
將性能分析結(jié)果保存到result文件
python -m cProfile -o result test.py
使用pstats來格式化顯示結(jié)果
python -c "import pstats; p=pstats.Stats('reslut); p.print_stats()" python -c "import pstats; p=pstats.Stats('result'); p.sort_stats('time').print_stats()
sort_stats支持以下參數(shù):
calls, cumulative, file, line, module, name, nfl, pcalls, stdname, time
測試示例:在代碼中直接使用profile與stats
import os def a(): sum = 0 for i in range(1, 10001): sum += i return sum def b(): sum = 0 for i in range(1, 100): sum += a() return sum print b() import cProfile# cProfile.run("b()") cProfile.run("b()", "result") import pstats pstats.Stats('result').sort_stats(-1).print_stats()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python之np.argmax()及對axis=0或者1的理解
這篇文章主要介紹了python之np.argmax()及對axis=0或者1的理解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06教你漂亮打印Pandas DataFrames和Series
在今天的文章中,我們將探討如何配置所需的pandas選項,這些選項將使我們能夠“漂亮地打印” pandas DataFrames,需要的朋友可以參考下2021-05-05Python爬蟲基礎(chǔ)之初次使用scrapy爬蟲實例
今天給大家?guī)淼氖顷P(guān)于Python爬蟲的相關(guān)知識,文章圍繞著Python scrapy展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06如何修復(fù)使用 Python ORM 工具 SQLAlchemy 時的常見陷阱
SQLAlchemy 是一個 Python ORM 工具包,它提供使用 Python 訪問 SQL 數(shù)據(jù)庫的功能。這篇文章主要介紹了如何修復(fù)使用 Python ORM 工具 SQLAlchemy 時的常見陷阱,需要的朋友可以參考下2019-11-11帶你用Python實現(xiàn)Saga 分布式事務(wù)的方法
在這篇文章里,我們介紹了 SAGA 的理論知識,也通過一個例子,完整給出了編寫一個 SAGA 事務(wù)的過程,涵蓋了正常成功完成,異常情況,以及成功回滾的情況,需要的朋友參考下吧2021-09-09