Python 如何计算多个cProfile结果的平均结果?

Python 如何计算多个cProfile结果的平均结果?,python,python-3.x,profiler,cprofile,Python,Python 3.x,Profiler,Cprofile,而不是像这样只运行一次配置文件: 导入cProfile def do_重型起吊(): 对于范围(100)内的i: 打印('你好') profiller=cProfile.Profile() profiller.enable() 做重物搬运 profiller.disable() profiller.print_stats(sort='time') 其中,配置文件结果如下所示: 502 function calls in 0.000 seconds Ordered by: inte

而不是像这样只运行一次配置文件:

导入cProfile
def do_重型起吊():
对于范围(100)内的i:
打印('你好')
profiller=cProfile.Profile()
profiller.enable()
做重物搬运
profiller.disable()
profiller.print_stats(sort='time')
其中,配置文件结果如下所示:

      502 function calls in 0.000 seconds

Ordered by: internal time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   100    0.000    0.000    0.000    0.000 {built-in method builtins.print}
   200    0.000    0.000    0.000    0.000 cp1252.py:18(encode)
   200    0.000    0.000    0.000    0.000 {built-in method _codecs.charmap_encode}
     1    0.000    0.000    0.000    0.000 test.py:2(do_heavy_lifting)
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
我想运行几次并打印平均结果,以提高精度

这是我最初想到的脚本配方:

导入cProfile
def do_重型起吊():
对于范围(100)内的i:
打印('你好')
def最佳配置文件(目标配置文件函数,计数):
profile_结果=[]
对于范围内的索引(计数):
profiller=cProfile.Profile()
profiller.enable()
目标配置文件函数()
profiller.disable()
profile_results.append(profiller)
配置文件\u结果/=计数
返回配置文件\u结果
重型提升结果=最佳利润(重型提升,10)
重载提升结果。打印统计数据(sort='time')
运行此操作后,它应该像第一个版本一样显示结果,但区别在于它们是多次运行的平均值,而不是一次运行

脚本草稿仍然缺少部分
profile_results/=count
,在所有迭代之后,我将获得所有计算结果,创建平均结果,并始终在屏幕上显示:

      502 function calls in 0.000 seconds

Ordered by: internal time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   100    0.000    0.000    0.000    0.000 {built-in method builtins.print}
   200    0.000    0.000    0.000    0.000 cp1252.py:18(encode)
   200    0.000    0.000    0.000    0.000 {built-in method _codecs.charmap_encode}
     1    0.000    0.000    0.000    0.000 test.py:2(do_heavy_lifting)
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

我使用函数
average()
创建了以下代码。我打开了
pstats
的实现,发现有一个名为
Stats.add()
的函数,它似乎只是将结果连接到当前对象中:

导入io
输入pstats
导入cProfile
def do_重型起吊():
对于范围(100)内的i:
打印('你好')
def平均值(统计、计数):
stats.total_calls/=计数
stats.prim_调用/=计数
统计总数_tt/=计数
对于func,源位于stats.stats.items()中:
cc、nc、tt、ct、呼叫者=源
stats.stats[func]=(cc/count、nc/count、tt/count、ct/count、调用者)
返回数据
def最佳配置文件(目标配置文件函数,计数):
输出\u流=io.StringIO()
profiller_status=pstats.Stats(流=输出流)
对于范围内的索引(计数):
profiller=cProfile.Profile()
profiller.enable()
目标配置文件函数()
profiller.disable()
profiller_status.add(profiller)
打印('Profiled','%.3f'%profiler\u status.total\u tt','seconds at',index,
'for',target\u profile\u函数。\u名称\u,flush=True)
平均值(profiller_状态、计数)
profiller\u status.sort\u stats(“时间”)
profiller_status.print_stats()
返回“\n%s的配置文件结果\n%s”%(
目标\配置文件\函数。\名称\输出\流。getvalue()
重型提升结果=最佳利润(重型提升,10)
打印(重型提升结果)
结果:

Profile results for do_heavy_lifting
         102.0 function calls in 0.001 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    100.0    0.001    0.000    0.001    0.000 {built-in method builtins.print}
      1.0    0.000    0.000    0.001    0.001 D:\test.py:5(do_heavy_lifting)
      1.0    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

你为什么不运行几次
do\u heavy\u lifting()