pythonprofilingbenchmarkingtimeitcprofile

Why does cProfile only run through the code once?


On the flip side, timeit runs through the code 1,000,000 times to get a reasonable asymptotic comparison to other code. cProfile only runs through the code once, and with only 3 decimal places in the results (0.000), it is not enough to get the full picture.

You get unuseful results like this:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
    1    0.000    0.000    0.000    0.000 <string>:1(<module>)
    1    0.000    0.000    0.000    0.000 a.py:27(function)
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Solution

  • Purpose of timeit is to get the throughput of a function, which will always require the code to run multiple times to fade out edge cases and give a good average.

    While cProfile, on the other hand, is used to profile each sub-call of the function's stack, to demystify all the magic happening inside a function.

    timeit will tell you that there's some optimisation required to function, while cProfile will point you to the right direction telling you which minuscule part of stack is hogging up your turnaround time.