Structured, zero-dependency Python timing for humans and telemetry.
Instantly isolate wall-clock latency from CPU burn across sync, async, and generators.
Quickstart
1. Measure Code Blocks (Wall vs. CPU)
Wrap any block in with Timer() as m: (or async with Timer() as m:). On exit, m.wall_time and m.cpu_time are immediately available:
import time
from timerun import Timer
with Timer() as m:
time.sleep(0.05) # Simulates network I/O or database wait
total = sum(i * i for i in range(100_000)) # Simulates CPU computation
print("Elapsed (Wall):", m.wall_time.timedelta) # e.g., 0:00:00.055120
print("Compute (CPU): ", m.cpu_time.timedelta) # e.g., 0:00:00.005110
Why both? If wall time is 2 seconds but CPU time is 5 milliseconds, your code spent 99.7% of its time waiting on I/O. If both are 2 seconds, you have a CPU bottleneck.
2. Time Functions and Generators
Use @Timer() on sync or async functions. The wrapped function retains a bounded deque of measurements:
from timerun import Timer
@Timer(maxlen=100)
def process_batch(items):
return [x * 2 for x in items]
process_batch([1, 2, 3])
last = process_batch.measurements[-1]
print("Wall time:", last.wall_time.timedelta)
print("CPU time: ", last.cpu_time.timedelta)
3. Stream to Logs & Observability
Use on_end to ship timings directly to your logs, OpenTelemetry, Prometheus, or Datadog without adding vendor dependencies to your core logic:
import logging
from timerun import Timer
logger = logging.getLogger("app")
with Timer(
metadata={"service": "checkout", "user_id": "u-123"},
on_end=lambda m: logger.info(
"checkout_completed",
extra={
"wall_ms": m.wall_time.duration / 1e6,
"cpu_ms": m.cpu_time.duration / 1e6,
**m.metadata,
},
),
):
pass # your operation
Installation
pip install timerun
Requires Python 3.10+.
Why TimeRun?
When code runs slowly, is it waiting on network/disk I/O, or burning CPU?
time.perf_counter()only gives a raw float of elapsed wall time. Measuring CPU time requires manualtime.thread_time()calls and boilerplate.- Profilers (
cProfile,py-spy) and APM agents (Datadog,New Relic) introduce binary dependencies, sampling overhead, or vendor lock-in. - Naive timing patterns risk race conditions and crosstalk in concurrent
asynciotasks or multi-threaded workers.
TimeRun provides clean, structured timing with sub-microsecond overhead:
| Feature | Manual time.perf_counter() |
Profilers (cProfile, py-spy) |
APM Agents (Datadog, New Relic) |
TimeRun |
|---|---|---|---|---|
| Dependencies | None | Extra tooling | Large binary agents | Zero (stdlib only) |
| Wall vs. CPU Breakdown | ❌ Manual boilerplate | ⚠️ Profiling overhead | ❌ Usually wall only | ✅ Built-in & precise |
| Structured Output | ❌ Raw floats | ❌ Binary trace files | ⚠️ Proprietary format | ✅ Structured Measurement |
| Asyncio & Thread Safe | ❌ Race condition risk | ⚠️ Global runtime cost | ✅ Handled by agent | ✅ Task & Thread isolated |
| Telemetry Hooks | ❌ Build it yourself | ❌ Offline analysis only | ⚠️ Vendor locked | ✅ Agnostic (on_end) |
| Footprint | 0 LOC | Heavy | Large background daemon | Single file (~500 LOC) |
Key Capabilities
- Zero Supply-Chain Risk: Pure Python standard library only (
time,dataclasses,contextvars); vendorable as a single file (timerun.py). - Context Isolation: Native
ContextVarstacks ensure concurrentasynciotasks and threads never mix up measurements. - Sub-Microsecond Overhead: Slotted dataclasses and integer nanoseconds keep internal overhead under ~0.4 µs.
- Full Async & Generator Support: Accurately times coroutines, synchronous generators, and asynchronous streaming generators across full iteration.
- 100% Test Coverage: Verified with comprehensive Behavior-Driven Development (BDD) test suites.
Documentation
- Documentation Site — Tutorials, concepts, and guides.
- API Guide — In-depth reference for
Timer,Measurement, andTimeSpan. - Cookbooks — Real-world recipes for FastAPI, OpenTelemetry, Structlog, and LLM streaming.
Contributing
Contributions are welcome! See CONTRIBUTING.md for setup, testing, and pull request guidelines.
License
This project is licensed under the MIT License — see the LICENSE file for details.
Metadata
Release files for timerun 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| timerun-0.7.0.tar.gz | 10.4 kB | With provenance | Details for timerun-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| timerun-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for timerun-0.7.0-py3-none-any.whl |
Total release size: 19.5 kB
Release files / timerun-0.7.0.tar.gz
| Download URL | timerun-0.7.0.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
df2ed8dd987d6334e12084cd6a8803df090c42ae4c656c6a8f10e76995e82932
|
|
BLAKE2b-256 checksum How to use checksums |
415c1b53f71d5359794f7f051cda112e716a27624c438e6f1f34689b4e202d60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logRelease files / timerun-0.7.0-py3-none-any.whl
| Download URL | timerun-0.7.0-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
acfa66d875e2d95a6f4586831d609d57280478ec5f6e21724e2d77ecf8d2fec5
|
|
BLAKE2b-256 checksum How to use checksums |
cbb8ecb09e9f49a672d02b1c1a64bd3cf2ef645aa9a6111429a8a7dd2bd68610
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logSecurity for timerun 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for timerun-0.7.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.