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.
- 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()
- 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.
- 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.