line_profiler.ipython_extension module

This module defines the %lprun and %%lprun_all IPython magic functions.

If you are using IPython, there is an implementation of an %lprun magic command which will let you specify functions to profile and a statement to execute. It will also add its LineProfiler instance into the __builtins__, but typically, you would not use it like that.

You can also use %%lprun_all, which profiles the whole cell you’re executing automagically, without needing to specify lines/functions yourself. It’s meant for easier use for beginners.

For IPython 0.11+, you can install it by editing the IPython configuration file ~/.ipython/profile_default/ipython_config.py to add the 'line_profiler' item to the extensions list:

c.TerminalIPythonApp.extensions = [
    'line_profiler',
]

Or explicitly call:

%load_ext line_profiler

To get usage help for %lprun and %%lprun_all, use the standard IPython help mechanism:

In [1]: %lprun?
class line_profiler.ipython_extension.LineProfilerMagics(**kwargs: Any)[source]

Bases: Magics

lprun(parameter_s='')[source]

Execute a statement under the line-by-line profiler from the line_profiler module.

Usage:

%lprun [<options>] <statement>

The given statement (which doesn’t require quote marks) is run via the LineProfiler. Profiling is enabled for the functions specified by the -f options. The statistics will be shown side-by-side with the code through the pager once the statement has completed.

Options:

-f <function>: LineProfiler only profiles functions and methods it is told to profile. This option tells the profiler about these functions. Multiple -f options may be used. The argument may be any expression that gives a Python function or method object. However, one must be careful to avoid spaces that may confuse the option parser.

-m <module>: Get all the functions/methods in a module

One or more -f or -m options are required to get any useful results.

-D <filename>: dump the raw statistics out to a pickle file on disk. The usual extension for this is .lprof. These statistics may be viewed later by running python -m line_profiler.

-T <filename>: dump the text-formatted statistics with the code side-by-side out to a text file.

-r: return the LineProfiler object after it has completed profiling.

-s: strip out all entries from the print-out that have zeros. This is an old, soon-to-be-deprecated alias for -z.

-z: strip out all entries from the print-out that have zeros.

-u: specify time unit for the print-out in seconds.

lprun_all(parameter_s='', cell='')[source]

Execute the whole notebook cell under the line-by-line profiler from the line_profiler module.

Usage:

%%lprun_all [<options>]

By default, without the -p option, it includes nested functions in the profiler. The statistics will be shown side-by-side with the code through the pager once the statement has completed.

Options:

-D <filename>: dump the raw statistics out to a pickle file on disk. The usual extension for this is .lprof. These statistics may be viewed later by running python -m line_profiler.

-T <filename>: dump the text-formatted statistics with the code side-by-side out to a text file.

-r: return the LineProfiler object after it has completed profiling.

-z: strip out all entries from the print-out that have zeros. This is included for consistency with the CLI.

-u: specify time unit for the print-out in seconds.

-t: store the total time taken (in seconds) to a variable called _total_time_taken in your notebook. This can be useful if you want to plot the total time taken for different versions of a code cell without needing to manually look at and type down the time taken. This can be accomplished with -r, but that would require a decent bit of boilerplate code and some knowledge of the timings data structure, so this is added to be beginner-friendly.

-p: Profile only top-level code (ignore nested functions). Using this can bypass any issues with ast transformations.

magics = {'cell': {'lprun_all': 'lprun_all'}, 'line': {'lprun': 'lprun'}}
registered = True