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

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.5.0.tar.gz (156.8 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.5.0-py3-none-any.whl (93.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: with_line_profiler-0.5.0.tar.gz
  • Upload date:
  • Size: 156.8 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.5.0.tar.gz
Algorithm Hash digest
SHA256 5bbe59b4c7f502f2fcfaf1d3b9420c6e10050c8e217ae3830c87c3322051c55c
MD5 b162efac26ce2eb460abc2ce78884f5f
BLAKE2b-256 655493d8da20cdd7104248090299f5db3d1f12a2548ebf8c6b68ccddd689f97a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for with_line_profiler-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32a6248111d3c9b3b09ccc5f58a6b224a9a5e1fd5095f549f4745ac04e8a6033
MD5 962f3983b478fece459e69971596df99
BLAKE2b-256 8ff9ee0cf1fe51558b7cbeb3e0fe0249d0fb82511dbd35cf67e508b65adcbbbf

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

0.6.0

2 files

This release

0.5.0 This release

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