line_profiler
Line-by-line timing for Python functions via an @profile decorator and the kernprof runner.
Repository Health
Technical Analysis
line_profiler is a profiler that reports how much time is spent on each individual line of your Python functions, rather than only aggregating by function like cProfile. You mark the functions you care about with an @profile decorator, run your program under the bundled kernprof script, and get a per-line breakdown of hits, time, and percentage of total for each decorated function.
This repository (under the pyutils organization) is the official, actively maintained continuation of Robert Kern’s original line_profiler. It includes a Cython/C timing core for low overhead, an IPython/Jupyter extension with %lprun, an autoprofile mode, and configuration via pyproject.toml, making it the standard tool for finding line-level hotspots in Python code.
What You Get
- An
@profiledecorator andLineProfilerclass for line-level timing - The
kernprofrunner that executes scripts with profiling enabled - Per-line reports of hit counts, total time, and percentage of function time
- An IPython/Jupyter
%lprunmagic for inline profiling - Autoprofile and pyproject.toml configuration for zero-decorator instrumentation
Common Use Cases
- Finding which specific lines inside a slow function dominate its runtime
- Profiling numerical or data-processing loops at line granularity
- Investigating hotspots inside a Jupyter notebook with %lprun
- Auto-instrumenting a module’s functions without editing source
Under The Hood
Architecture - The core timing engine is a Cython extension (_line_profiler.pyx) backed by C trace callbacks (c_trace_callbacks.c) and a portable timer (timers.c, derived from CPython’s _lsprof). The Python layer wraps this in line_profiler.py (the LineProfiler class), explicit_profiler.py (the @profile decorator), an autoprofile/ package for decorator-free instrumentation, ipython_extension.py for the %lprun magic, and toml_config.py for configuration. kernprof is the standalone runner script.
Tech Stack - Python plus Cython/C for the low-overhead line tracer, built via CMake/scikit-build (CMakeLists.txt) and packaged with type stubs (py.typed, .pyi). Docs are hosted on Read the Docs.
Code Quality - As the official maintained fork it is actively developed with a comprehensive test suite, CI (CircleCI/GitHub Actions), and Codecov coverage. The code is modular, typed, and separates the C timing core cleanly from the Python-facing API.
API Design - The workflow is well-trodden and ergonomic: decorate with @profile, run with kernprof -l, view with python -m line_profiler. The %lprun magic and autoprofile lower friction further, and per-line output is easy to read, though understanding kernprof’s run model takes a short ramp.