cProfile使用
run
import cProfile
def predict():
....
cProfile.run('predict()')
# 输出
155353 function calls (154818 primitive calls) in 6.252 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:1017(_handle_fromlist)
1 0.089 0.089 6.252 6.252 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 BlpImagePlugin.py:258(_accept)
其中
ncalls
表示调用次数tottime
表示在给定函数中花费的总时间(不包括调用子函数的时间)percall
表示单词调用给定函数花费的时间cumtime
表示在这个函数和所有子函数中花费的累积时间(从调用到退出)percall
表示调用一次这个函数内所有函数所用的时间filename:lineno(function)
表示每个函数的各自信息
保存数据
cProfile.run('predict()', 'result')
就会保存到当前目录下的result文件
查看性能数据
import pstats
from pstats import SortKey
# 加载保存到restats文件中的性能数据
p = pstats.Stats('result')
# 打印所有统计信息
p.strip_dirs().sort_stats(-1).print_stats()
# 打印累计耗时最多的10个函数
p.sort_stats(SortKey.CUMULATIVE).print_stats(10)
# 打印内部耗时最多的10个函数(不包含子函数)
p.sort_stats(SortKey.TIME).print_stats(10)
# 打印包含load的函数名的调用者统计信息
p.print_callers(.5, 'normalize')
# 按耗时排序,依次打印类的__init__方法的统计信息
p.sort_stats(SortKey.CUMULATIVE).print_stats('__init__')
References
最后编辑于:2023 年 06 月 25 日 10:14