%prun -l 7 -s cumulative function()
1.cProfile模块,输出结果按函数名排序.
cProfile记录的是各函数调用的起始和结束时间,并依次计算总时间
python -m cProfile XX.py
-s指定排序规则:
python -m cProfile -s cumulative xx.py
利用%run -p
%run -p -s cumulative xx.py
2.cProfile还可以分析任意代码块的内容
1)%prun 分析的是python语句,-l +整数 是指定输出几行的意思
%prun -l 7 -s cumulative function()
二、逐行分析函数的性能
对一个或多个函数逐行性能分析,例如对add_and_sum进行分析
%lprun -f add_and_sum add_and_sum(x,y)
通用语法是这样的:
%lprun -f func1 -f func2 statement_to_profile
通常,我会用%prun(cProfile)做宏观的性能分析,而用%lprun做微观的性能分析。在使用%lprun时,之所以必须显式指明待测试的函数名,是因为“跟踪”每一行代码的执行所需开销很大,对不感兴趣的函数跟踪会对结果造成显著影响。
原文:https://www.cnblogs.com/sggggr/p/12187332.html