Python的Profile概述
生活随笔
收集整理的這篇文章主要介紹了
Python的Profile概述
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python的profile是一組統計功能,用以描述Python應用的各個部分的執行狀態、頻率和時長等。
根據統計的采樣,可以將profile分為如下兩大類:
- deterministic profiling,通過采集全部數據,給出的確定數據
- statistical profiling,通過隨機采樣,給出的推理數據
1. 在Python標準庫中,提供了deterministic?profile的如下兩個實現
1) cProfile,一個Python模塊,推薦用戶直接使用
- class Profile(_lsprof.Profiler)
import cProfile cProfile.run('a_call_to_func("para")') cProfile.run('a_call_to_func("para")', 'mystats')
命令行中的使用示例:
python3 -m cProfile [-o my_output_file] [-s sort_order] myscript.py
其中,參數sort_order表示輸出的排序依據,其可能值如下:
- '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
- def run(statement, filename=None, sort=-1)
- def runctx(statement, globals, locals, filename=None, sort=-1)
- class Profile
import profile profile.Profile(my_time_func)
import profile pr = profile.Profile() for i in range(5):print(pr.calibrate(10000))
2. 為便于查看,在Python標準庫提供了pstats模塊,可以將Python應用的profile格式化輸出為報表
- class Stats
import pstats p = pstats.Stats('mystats') p.strip_dirs().sort_stats(-1).print_stats()
參考鏈接:
https://docs.python.org/3.5/library/profile.html
https://github.com/python/cpython/blob/3.5/Lib/cProfile.py
https://github.com/python/cpython/blob/3.5/Lib/profile.py
https://github.com/python/cpython/blob/3.5/Lib/pstats.py
總結
以上是生活随笔為你收集整理的Python的Profile概述的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSM + Ajax
- 下一篇: python移动自动化测试面试视频_Py