line_profiler

line_profiler is a line-by-line performance profiler for Python that measures execution time for each line within selected functions and reports detailed timing statistics.

Installation and Setup

Install from PyPI:

Windows PowerShell
PS> py -m pip install line_profiler
Shell
$ python -m pip install line_profiler

If you want the IPython/Jupyter magics too, install the extra:

Shell
$ python -m pip install "line_profiler[ipython]"

Key Features

  • Provides line-by-line timing reports with hit counts, total time, per-hit time, and per-line percentage breakdown.
  • Ships the kernprof runner to execute your program and write profiling results to a .lprof file.
  • Offers a CLI viewer to inspect .lprof output and control display options like output units.
  • Integrates with IPython and Jupyter via %load_ext line_profiler and %lprun for ad hoc profiling in notebooks.
  • Provides a Python API for profiling function calls directly in code and printing stats with a chosen output_unit.

Usage

Mark functions to profile by adding the @profile decorator:

Python script.py
from line_profiler import profile

@profile
def work(n=10000):
    total = 0
    for i in range(n):
        total += i * i
    return total

if __name__ == "__main__":
    work()

The quickest way to run the profiler agains the script is to use the LINE_PROFILE environment variable:

Shell
$ LINE_PROFILE=1 python script.py
Timer unit: 1e-09 s

  0.00 seconds - .../script.py:4 - work
Wrote profile results to profile_output.txt
Wrote profile results to profile_output_2025-12-15T124644.txt
Wrote profile results to profile_output.lprof
To view details run:
python -m line_profiler -rtmz profile_output.lprof

View the results:

Shell
python -m line_profiler -rtmz profile_output.lprof
Timer unit: 1e-06 s

Total time: 0.004837 s
File: .../script.py
Function: work at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           @profile
     5                                           def work(n=10000):
     6         1          1.0      1.0      0.0      total = 0
     7     10001       2466.0      0.2     51.0      for i in range(n):
     8     10000       2369.0      0.2     49.0          total += i * i
     9         1          1.0      1.0      0.0      return total

  0.00 seconds - .../script.py:4 - work

Run with kernprof and save the results to a .lprof file, then display them:

Shell
$ kernprof -l script.py
Wrote profile results to 'script.py.lprof'
Inspect results with:
python -m line_profiler -rmt script.py.lprof

$ python -m line_profiler script.py.lprof
Timer unit: 1e-06 s

Total time: 0.003005 s
File: script.py
Function: work at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           @profile
     5                                           def work(n=10000):
     6         1          0.0      0.0      0.0      total = 0
     7     10001       1431.0      0.1     47.6      for i in range(n):
     8     10000       1573.0      0.2     52.3          total += i * i
     9         1          1.0      1.0      0.0      return total

  0.00 seconds - script.py:4 - work

Use the programmatic API:

Python fib.py
from line_profiler import LineProfiler

def fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

lp = LineProfiler()
lp.add_function(fib)
lp_wrapper = lp(fib)
lp_wrapper(100000)
lp.print_stats(output_unit=1e-3)

Tutorial

Profiling in Python: How to Find Performance Bottlenecks

In this tutorial, you'll learn how to profile your Python programs using numerous tools available in the standard library, third-party libraries, as well as a powerful tool foreign to Python. Along the way, you'll learn what profiling is and cover a few related concepts.

intermediate tools

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Dec. 15, 2025