%%timeit and %timeit in IPython
Profiling string concatanation:
Profiling loops over iterables and lists:
line_profiler in command line
The source code with @profile directive before the function we want to profile:
Using kernprof command to calculate profiling line by line
Page request is almost always slower than any calculation based on the information on the page.
timeit command line
Profiling concatanation of numbers
Profiling repetition of elements in an array
Using cProfile (Preferred Profiler)
Python includes a profiler called cProfile. This is generally preferred over using timeit.
It breaks down your entire script and for each method in your script it tells you:
ncalls: The number of times a method was called
tottime: Total time spent in the given function (excluding time made in calls to sub-functions)
percall: Time spent per call. Or the quotient of tottime divided by ncalls
cumtime: 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 calls
filename:lineno(function): provides the respective data of each function
The cProfiler can be easily called on Command Line using:
To sort the returned list of profiled methods by the time taken in the method:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors
and released under CC BY-SA 3.0