Skip to main content

with-line-profiler

PyPI CI License: MIT Python

Two independent profiling tools in one distribution: line-by-line tracing for a region you suspect, and low-overhead phase accounting for a run too long to trace. Zero dependencies on Python 3.11+, MIT licensed.

pip install with-line-profiler
# example.py
from lineprofiler import LineProfiler

def slow_function():
    total = 0
    for i in range(1_000_000):
        total += i * i
    return total

profiler = LineProfiler()
with profiler:
    slow_function()
profiler.print_stats()
====================================================================================================
File: /path/to/example.py
Function: slow_function at line 3
Total time: 710676.0 µs
====================================================================================================
Line #   Hits       Time (µs)       Per Hit (µs)    % Time     Line Content
----------------------------------------------------------------------------------------------------
6        1000000    364848.9        0.4             51.3       total += i * i
5        1000001    345825.8        0.3             48.7       for i in range(1_000_000):
7        1          0.7             0.7             0.0        return total
4        1          0.6             0.6             0.0        total = 0

(numbers vary by machine; File: prints the absolute path)

No decorators to add, no separate kernprof run, no build step. Only code under your project folder is traced — the folder is auto-detected by walking up to the nearest .git — so the output is your code, not the standard library.

Want a picture instead? profiler.to_html("profile.html") writes an annotated, heat-coloured source view as a single self-contained file.

Which tool

Tool What it does Use it when
lineprofiler.LineProfiler Line-by-line tracing for a bounded region, scoped to your project folder. You have narrowed the problem down and want per-line timings inside it.
lineprofiler.accounting Semantic accounting for regions you name. Aggregates only — counts, sums, a fixed-bucket histogram — at ~2 µs per phase, across every process in a pipeline. You are profiling a long, multi-process training run and need to know which phase, which role and which node the time went to.

They share nothing but the distribution: accounting never imports LineProfiler. If you arrived here for a training run, you want the accounting layer — it is the one built to stay enabled for twelve hours.

from lineprofiler.accounting import start, stop

start(role="actor")     # or Profiler(...) / with profiler.phase(...)
...
stop()
lineprofiler report profile/                        # the run, as a table
lineprofiler report profile/ --format html -o r.html # ...or as a page you can share

Why was that worker idle?

A report says queue_get was 80% wait. It cannot say when, or what for — a total has no position on a clock. Turn on the timeline and the answer is a picture:

Profiler(run_dir="profile", role="actor", trace=True)
lineprofiler trace profile/ -o trace.html

One lane per worker on a shared clock, idle time drawn as absence, and — where you mark a queue with signal() / wait_on() — arrows from a producer to the consumer it unblocked, plus the critical path that actually set the run's length.

Nothing to instrument first: LINEPROFILER_TRACE=auto derives the lanes from function calls in your project, with no change to your code at all.

Documentation

Python support

3.10 and newer. On 3.12+ the line profiler uses sys.monitoring, so it can run alongside coverage.py, pdb and other tracing tools; below that it falls back to sys.settrace, which is a single global hook and cannot. tomli is required only on 3.10, where tomllib is not yet in the standard library.

Licence

MIT

The claude.md is partially created from https://github.com/multica-ai/andrej-karpathy-skills/blob/main/CLAUDE.md

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

with_line_profiler-0.6.0.tar.gz (205.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

with_line_profiler-0.6.0-py3-none-any.whl (129.6 kB view details)

Uploaded Python 3

File details

Details for the file with_line_profiler-0.6.0.tar.gz.

File metadata

  • Download URL: with_line_profiler-0.6.0.tar.gz
  • Upload date:
  • Size: 205.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for with_line_profiler-0.6.0.tar.gz
Algorithm Hash digest
SHA256 eb38279f78e330d8d8a2fb80fe73c41a5116f1b5d832ec585b49cb2b01fd89f8
MD5 731e3b6a527d1c74a47d1682ef0e7fbd
BLAKE2b-256 776dc8d4113495b43db4e14ad241b5361c31ef8a9963454bbca6fe54c78df623

See more details on using hashes here.

File details

Details for the file with_line_profiler-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for with_line_profiler-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b550d95e39a16b109d6b8d4e3d668f613116a0f14fd081c9bd8f21bebb200900
MD5 41d2ff7fb6664aa3935333203d64ef08
BLAKE2b-256 5947891ab32076d502eb527cb12b817946d489bd5027e26f89f6bca295fcad6f

See more details on using hashes here.

Release history Release notifications | RSS feed

0.8.2

2 files

0.8.0

2 files

0.7.1

2 files

This release

0.6.0 This release

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page