line_profiler.line_profiler module

This module defines the core LineProfiler class as well as methods to inspect its output. This depends on the line_profiler._line_profiler Cython backend.

line_profiler.line_profiler.load_ipython_extension(ip)[source]

API for IPython to recognize this module as an IPython extension.

line_profiler.line_profiler.is_coroutine(f)[source]
line_profiler.line_profiler.is_generator(f)[source]

Return True if a function is a generator.

line_profiler.line_profiler.is_classmethod(f)[source]
class line_profiler.line_profiler.LineProfiler[source]

Bases: LineProfiler

A profiler that records the execution times of individual lines.

This provides the core line-profiler functionality.

Example

>>> import line_profiler
>>> profile = line_profiler.LineProfiler()
>>> @profile
>>> def func():
>>>     x1 = list(range(10))
>>>     x2 = list(range(100))
>>>     x3 = list(range(1000))
>>> func()
>>> profile.print_stats()
wrap_classmethod(func)[source]

Wrap a classmethod to profile it.

wrap_coroutine(func)[source]

Wrap a Python 3.5 coroutine to profile it.

wrap_generator(func)[source]

Wrap a generator to profile it.

wrap_function(func)[source]

Wrap a function to profile it.

dump_stats(filename)[source]

Dump a representation of the data to a file as a pickled LineStats object from get_stats().

print_stats(stream=None, output_unit=None, stripzeros=False, details=True, summarize=False, sort=False, rich=False)[source]

Show the gathered statistics.

run(cmd)[source]

Profile a single executable statment in the main namespace.

runctx(cmd, globals, locals)[source]

Profile a single executable statement in the given namespaces.

runcall(func, *args, **kw)[source]

Profile a single function call.

add_module(mod)[source]

Add all the functions in a module and its classes.

line_profiler.line_profiler.is_ipython_kernel_cell(filename)[source]

Return True if a filename corresponds to a Jupyter Notebook cell

line_profiler.line_profiler.show_func(filename, start_lineno, func_name, timings, unit, output_unit=None, stream=None, stripzeros=False, rich=False)[source]

Show results for a single function.

Parameters:
  • filename (str) – path to the profiled file

  • start_lineno (int) – first line number of profiled function

  • func_name (str) – name of profiled function

  • timings (List[Tuple[int, int, float]]) – measurements for each line (lineno, nhits, time).

  • unit (float) – The number of seconds used as the cython LineProfiler’s unit.

  • output_unit (float | None) – Output unit (in seconds) in which the timing info is displayed.

  • stream (io.TextIOBase | None) – defaults to sys.stdout

  • stripzeros (bool) – if True, prints nothing if the function was not run

  • rich (bool) – if True, attempt to use rich highlighting.

Example

>>> from line_profiler.line_profiler import show_func
>>> import line_profiler
>>> # Use a function in this file as an example
>>> func = line_profiler.line_profiler.show_text
>>> start_lineno = func.__code__.co_firstlineno
>>> filename = func.__code__.co_filename
>>> func_name = func.__name__
>>> # Build fake timeings for each line in the example function
>>> import inspect
>>> num_lines = len(inspect.getsourcelines(func)[0])
>>> line_numbers = list(range(start_lineno + 3, start_lineno + num_lines))
>>> timings = [
>>>     (lineno, idx * 1e13, idx * (2e10 ** (idx % 3)))
>>>     for idx, lineno in enumerate(line_numbers, start=1)
>>> ]
>>> unit = 1.0
>>> output_unit = 1.0
>>> stream = None
>>> stripzeros = False
>>> rich = 1
>>> show_func(filename, start_lineno, func_name, timings, unit,
>>>           output_unit, stream, stripzeros, rich)
line_profiler.line_profiler.show_text(stats, unit, output_unit=None, stream=None, stripzeros=False, details=True, summarize=False, sort=False, rich=False)[source]

Show text for the given timings.

line_profiler.line_profiler.load_stats(filename)[source]

Utility function to load a pickled LineStats object from a given filename.

line_profiler.line_profiler.main()[source]

The line profiler CLI to view output from kernprof -l.