Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between tottime and cumtime on cProfile output?

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?

like image 791
takahashi Avatar asked Nov 03 '16 14:11

takahashi


People also ask

How does Python cProfile work?

cProfile: A deterministic profiler It works by tracing every function call in a program. That's why it's a deterministic profiler: if you run it with the same inputs it'll give the same output. By default cProfile measures wallclock time—how much time elapsed during the function run.

How do you profile a Python code using cProfile?

The syntax is cProfile. run(statement, filename=None, sort=-1) . You can pass python code or a function name that you want to profile as a string to the statement argument. If you want to save the output in a file, it can be passed to the filename argument.


1 Answers

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.

like image 127
Martijn Pieters Avatar answered Oct 12 '22 10:10

Martijn Pieters