Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read the output of the IPython %prun (profiler) command?

I run this:

In [303]: %prun my_function()          384707 function calls (378009 primitive calls) in 83.116 CPU seconds     Ordered by: internal time     ncalls  tottime  percall  cumtime  percall filename:lineno(function)     37706   41.693    0.001   41.693    0.001 {max}     20039   36.000    0.002   36.000    0.002 {min}     18835    1.848    0.000    2.208    0.000 helper.py:119(fftfreq) 

--snip--

What do each of tottime, percall, cumtime? ncalls is fairly obviously (number of times the function is called). My guess is that tottime is the total time spent in the function excluding time spent within its own function calls; percall is ???; cumtime is total time spent in the function call including time spent within its own function calls (but of course, excluding double counting). The docs are not too helpful; Google search doesn't help either.

like image 387
Peter D Avatar asked Aug 15 '11 19:08

Peter D


People also ask

How do I get the document of command in IPython?

For this, simply type %run -d myscript at an IPython prompt. See the %run command's documentation for more details, including how to control where pdb will stop execution first. For more information on the use of the pdb debugger, see Debugger Commands in the Python documentation.

What is IPython command?

IPython bridges this gap, and gives you a syntax for executing shell commands directly from within the IPython terminal. The magic happens with the exclamation point: anything appearing after ! on a line will be executed not by the Python kernel, but by the system command-line.

What is %% capture in Python?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

What are magic commands in IPython?

The magic commands, or magics, are handy commands built into the IPython kernel that make it easy to perform particular tasks, for example, interacting Python's capabilities with the operating system, another programming language, or a kernel. IPython provides two categories of magics: line magics and cell magics.


1 Answers

It's just a convenient wrapper for Python's own profiler, the documentation for which is here:

http://docs.python.org/library/profile.html#module-pstats

Quoting:

ncalls for the number of calls,

tottime for the total time spent in the given function (and excluding time made in calls to sub-functions),

percall is the quotient of tottime divided by ncalls

cumtime is the total 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

like image 139
Thomas K Avatar answered Sep 23 '22 01:09

Thomas K