代码性能解析,显示运行时间情况
示例;
import profile
def r():
print(12222*123344)
def ra():
print(1)
def rb():
r()
ra()
profile.run("rb()")
profile输出
line_profiler 一行一行的分析性能
9 function calls in 0.016 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 :0(exec)
2 0.000 0.000 0.000 0.000 :0(print)
1 0.016 0.016 0.016 0.016 :0(setprofile)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.016 0.016 profile:0(rb())
1 0.000 0.000 0.000 0.000 test1.py:3(r)
1 0.000 0.000 0.000 0.000 test1.py:5(ra)
●ncalls:表示函数调用的次数;
●tottime:表示指定函数的总的运行时间,除掉函数中调用子函数的运行时间;
●percall:(第一个 percall)等于 tottime/ncalls;
●cumtime:表示该函数及其所有子函数的调用运行的时间,即函数开始调用到返回的时间;
●percall:(第二个 percall)即函数运行一次的平均时间,等于 cumtime/ncalls;
●filename:lineno(function):每个函数调用的具体信息;
如果需要将输出以日志的形式保存,只需要在调用的时候加入另外一个参数。如 profile.run(“profileTest()”,”testprof”)。