prun(self, parameter_s='', cell=None)
Usage, in line mode:
%prun [options] statement
Usage, in cell mode:
%%prun [options] [statement] code... code...
In cell mode, the additional code lines are appended to the (possibly empty) statement in the first line. Cell mode allows you to easily profile multiline blocks without having to put them in a separate function.
The given statement (which doesn't require quote marks) is run via the python profiler in a manner similar to the profile.run() function. Namespaces are internally managed to work correctly; profile.run cannot be used in IPython because it makes certain assumptions about namespaces which do not hold under IPython.
Options:
-l <limit>
you can place restrictions on what or how much of the profile gets printed. The limit value can be:
A string: only information for function names containing this string is printed.
An integer: only these many lines are printed.
A float (between 0 and 1): this fraction of the report is printed (for example, use a limit of 0.4 to see the topmost 40% only).
You can combine several limits with repeated use of the option. For example, -l __init__ -l 5
will print only the topmost 5 lines of information about class constructors.
-r
return the pstats.Stats object generated by the profiling. This object has all the information about the profile in it, and you can later use it for further analysis or in other functions.
-s <key>
sort profile by given key. You can provide more than one key by using the option several times: '-s key1 -s key2 -s key3...'. The default sorting key is 'time'.
The following is copied verbatim from the profile documentation referenced below:
When more than one key is provided, additional keys are used as secondary criteria when the there is equality in all keys selected before them.
Abbreviations can be used for any key names, as long as the abbreviation is unambiguous. The following are the keys currently defined:
============ ===================== Valid Arg Meaning ============ ===================== "calls" call count "cumulative" cumulative time "file" file name "module" file name "pcalls" primitive call count "line" line number "name" function name "nfl" name/file/line "stdname" standard name "time" internal time ============ =====================
Note that all sorts on statistics are in descending order (placing most time consuming items first), where as name, file, and line number searches are in ascending order (i.e., alphabetical). The subtle distinction between "nfl" and "stdname" is that the standard name is a sort of the name as printed, which means that the embedded line numbers get compared in an odd way. For example, lines 3, 20, and 40 would (if the file names were the same) appear in the string order "20" "3" and "40". In contrast, "nfl" does a numeric compare of the line numbers. In fact, sort_stats("nfl") is the same as sort_stats("name", "file", "line").
-T <filename>
save profile results as shown on screen to a text file. The profile is still shown on screen.
-D <filename>
save (via dump_stats) profile statistics to given filename. This data is in a format understood by the pstats module, and is generated by a call to the dump_stats() method of profile objects. The profile is still shown on screen.
-q
suppress output to the pager. Best used with -T and/or -D above.
If you want to run complete programs under the profiler's control, use %run -p [prof_opts] filename.py [args to program]
where prof_opts contains profiler specific options as described here.
You can read the complete documentation for the profile module with:
In [1]: import profile; profile.help()
User variables are no longer expanded, the magic line is always left unmodified.
Run a statement through the python code profiler.
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them