What does it mean when the cumtimes of callees don't add up to the total cumtime of a function?
<p>I'm using <code>cProfile.Profile</code>.</p>

<p>In the output of <code>print_callees</code>, I can see that a function takes about 2 seconds of <code>cumtime</code>.</p>

<p>But when I check the output of the functions it calls, their <code>cumtime</code> don't sum up to that of the caller. Actually it's much smaller.</p>

<p>What could be the reason of this?</p>

<p>This means that the function performs computations (and most probably contains a loop).</p>

<p>Example:</p>

<pre><code>def foo(n):
    s = 0
    for i in range(n):
        s += i
    print(s)
    return s
</code></pre>

<p><code>print()</code> is a called by <code>foo()</code>, but for a large enough <code>n</code> more time is spent in the <code>for</code> loop (and thus in <code>foo()</code> proper) than in <code>print()</code>. Thus <code>cumtime</code> of <code>foo()</code> should significantly exceed <code>cumtime</code> of <code>print()</code>.</p>

python performance