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.8.0.tar.gz (249.3 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.8.0-py3-none-any.whl (156.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: with_line_profiler-0.8.0.tar.gz
  • Upload date:
  • Size: 249.3 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.8.0.tar.gz
Algorithm Hash digest
SHA256 cb0537bd87b7a8dd42f9cd3c905195d8ac5970909f9d74da5fed4aebfc3c64b1
MD5 8c334fe128abf2dbfc4734a813a9075a
BLAKE2b-256 2b162053193478df12abf27f67341706818791262b6f5c56eb0711b0175ab4ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for with_line_profiler-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 96bdb2bf121877a316e3fbc9825cc324dfe682043a462dfa57b55e73df5dca3d
MD5 6a2b07a435c4bf34da729335f0a93329
BLAKE2b-256 46c9aa3e9180ca90f00dc07816faab4bd22fa0f72db158dc9bb691f6a305c6d5

See more details on using hashes here.

Release history Release notifications | RSS feed

0.8.2

2 files

This release

0.8.0 This release

2 files

0.7.1

2 files

0.6.0

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