Python Profiling Cheat Sheet
On the commandline:
where the argument to -s
can be many other things:
calls
: call countcumulative
: cumulative timecumtime
: cumulative timefile
: file namefilename
: file namemodule
: file namencalls
: call countpcalls
: primitive call countline
: line numbername
: function namenfl
: name/file/linestdname
: standard nametime
: internal timetottime
: internal time
For more details, see https://docs.python.org/2/library/profile.html#pstats.Stats.sort_stats
In IPython:
You could time a single run with %time
, or calculate average time spent over
multiple runs with %timeit
, and prun
for profiling. e.g.
%time
In [13]: %time 1 + 1 CPU times: user 4 µs, sys: 1 µs, total: 5 µs Wall time: 6.91 µs Out[13]: 2
%timeit
In [14]: %timeit 1 + 1 100000000 loops, best of 3: 14.2 ns per loop
%prun
There are also
%lprun
, which can profile each line of a script, and%mpruun
which can profile memory usage of a script
But they need external modules
line-profiler
and
memory_profiler
installed. I
haven’t used the two extensively, but it may be worthwhile to checkout. See this
post for some usage
examples.