cProfile 输出上的 tottime 和 cumtime 有什么区别? [英] What is the difference between tottime and cumtime on cProfile output?

查看:136
本文介绍了cProfile 输出上的 tottime 和 cumtime 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 cProfile 和以下命令分析 python 脚本 main.py:

python -m cProfile -s tottime main.py

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

13.597 秒内 10184337 次函数调用(10181667 次原始调用)订购者:内部时间ncalls tottime percall cumtime percall filename:lineno(function)1 4.674 4.674 13.598 13.598 main.py:2(<模块>)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) 与 main.pycumtime (13.598) 有何不同,因为这个函数(即整个脚本)只调用一次?

解决方案

tottime单独在函数中花费的总时间.cumtime 是函数花费的总时间加上这个函数调用的所有函数.

如果一个函数从不调用其他任何东西,这两个值将是相同的.例如,{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 秒.

来自文档:><块引用>

tottime
在给定函数中花费的总时间(不包括调用子函数的时间)

[...]

cumtime
是在这个函数和所有子函数中花费的累积时间(从调用到退出).即使对于递归函数,这个数字也是准确的.

I am profiling a python script main.py using cProfile with the following command:

python -m cProfile -s tottime main.py

The output I get is (only copy-pasted the top lines of the output):

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}

...

How can the tottime (4.674) be different from the cumtime (13.598) for main.py, since this function (ie. the whole script) is only called once?

解决方案

tottime is the total time spent in the function alone. cumtime is the total time spent in the function plus all functions that this function called.

The two values is going to be the same if a function never calls anything else. For example, {waitKey} doesn't appear to invoke anything else:

  459    2.381    0.005    2.381    0.005 {waitKey}

but getPacket() calls other functions, so it's cumtime column includes the time for those calls:

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

The main.py line covers all code run outside of functions, the global code; just the statements at that level took 4.674 seconds to run, but because those statements called other functions, the total cumulative time of the main.py code plus all function calls made is 13.598 seconds.

From the documentation:

tottime
for the total time spent in the given function (and excluding time made in calls to sub-functions)

[...]

cumtime
is the cumulative time spent in this and all subfunctions (from invocation till exit). This figure is accurate even for recursive functions.

这篇关于cProfile 输出上的 tottime 和 cumtime 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆