line_profiler

Line-by-line timing for Python functions via an @profile decorator and the kernprof runner.

Library
PyPI
v5.0.2
3,242stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
78/100Good
Development Activity84
Maintenance72
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
83/100Excellent
Architecture85
Code Quality85
Innovation80
Learning Curve80

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 @profile decorator and LineProfiler class for line-level timing
  • The kernprof runner that executes scripts with profiling enabled
  • Per-line reports of hit counts, total time, and percentage of function time
  • An IPython/Jupyter %lprun magic 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search