cProfile输出上的tottime和cumtime有什么区别?

问题描述 投票:60回答:1

我正在使用cProfile通过以下命令来分析python脚本main.py

python -m cProfile -s tottime main.py

我得到的输出是(仅复制粘贴输出的第一行):

10184337 function calls (10181667 primitive calls) in 13.597 seconds

Ordered by: internal time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    4.674    4.674   13.598   13.598 main.py:2(<module>)
 2142    2.964    0.001    4.663    0.002 load_aerdat3.py:61(getPacket)
  459    2.381    0.005    2.381    0.005 {waitKey}
1667989    1.170    0.000    1.170    0.000 {numpy.core.multiarray.array}

...

tottime(4.674)与cumtimemain.py(13.598)有何不同,因为此函数(即整个脚本)仅被调用一次?

python profiling cprofile
1个回答
84
投票

tottime是花费的总时间仅在函数中cumtime是函数plus该函数调用的所有函数所花费的总时间。

如果一个函数从不调用其他任何东西,则两个值将相同。例如,{waitKey}似乎没有调用其他任何东西:

  459    2.381    0.005    2.381    0.005 {waitKey}

但是getPacket()会调用其他函数,因此cumtime列中包括这些调用的时间:

 2142    2.964    0.001    4.663    0.002 load_aerdat3.py:61(getPacket)

main.py行涵盖了在函数外部运行的所有代码,即全局代码;仅该级别的语句花费了4.674秒运行,但是由于这些语句调用了其他函数,因此main.py代码的总累积时间加上所有进行的函数调用为13.598秒。

documentation

tottime给定功能所花费的总时间(不包括调用子功能所花费的时间)

[...]

cumtime是此功能和所有子功能(从调用到退出)花费的累积时间。该数字甚至对于递归函数也是准确的。

© www.soinside.com 2019 - 2024. All rights reserved.