with-line-profiler
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
- The line profiler — the
withblock,start_profiling(), and what it does not do - The accounting layer — phases, counters, and instrumenting without threading an argument
- Accounting recipes — reading the report, I/O and GPU bottlenecks, overhead budgets, exporting to W&B
- Multiple processes and nodes — Slurm, forking, preemption, heavy backends
- HTML reports — the icicle chart, the trace timeline, the annotated source view, and the embedded data block
- Configuration — environment variables,
[tool.lineprofiler], optional dependencies - Comparison with other profilers —
line_profiler, py-spy, Scalene, VizTracer, and when to use those instead
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file with_line_profiler-0.7.1.tar.gz.
File metadata
- Download URL: with_line_profiler-0.7.1.tar.gz
- Upload date:
- Size: 236.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f285b55c56b7a097f7241f19482668a9f7dfc6eff23582263ff2a3f460b14b7
|
|
| MD5 |
299eb613438a1ad8c5ff9b6644f70292
|
|
| BLAKE2b-256 |
9f6070da5a8a61342dd19b462222ff74696f55cc46909e06d413c5d43e95798d
|
File details
Details for the file with_line_profiler-0.7.1-py3-none-any.whl.
File metadata
- Download URL: with_line_profiler-0.7.1-py3-none-any.whl
- Upload date:
- Size: 147.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c64c3c299f12a8bcf833ff89fc4c5dc29bf0efd3ec43288df5f495cae97859f0
|
|
| MD5 |
79b7049b71a92a7f9cacb34712fe5bd4
|
|
| BLAKE2b-256 |
c70dd74e31550aae0d3dbed478327bad91aff1e1971b2576544b729d59bfd711
|