Using cProfile Preferred Profiler
suggest changePython includes a profiler called cProfile. This is generally preferred over using timeit. They answer different questions: cProfile breaks down a script, and timeit times a single statement.
It breaks down your entire script and for each function in your script it tells you:
ncalls: The number of times a method was calledtottime: Total time spent in the given function (excluding time spent in calls to sub-functions)percall: Time spent per call. Or the quotient of tottime divided by ncallscumtime: The cumulative time spent in this and all subfunctions (from invocation till exit). This figure is accurate even for recursive functions.percall: is the quotient of cumtime divided by primitive callsfilename:lineno(function): provides the respective data of each function
cProfile can be called from the command line:
$ python -m cProfile main.py
To sort the returned list of profiled functions by the time spent inside each function:
$ python -m cProfile -s time main.py
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents