Skip to main content

Tiny dependency-free timing decorator, context manager, and stopwatch utility.

Project description

timerx

Version Python License: MIT Tests Dependencies

Tiny dependency-free timing utilities for Python. Use it as a decorator, context manager, or named stopwatch, then inspect cumulative stats or print a compact summary.

Install

pip install timerx

For local development:

pip install -e ".[test]"
pytest

Quick Start

import timerx


@timerx.track
def train_model():
    ...


@timerx.track(name="custom_step")
def transform():
    ...


with timerx.lap("preprocessing"):
    ...


timerx.start("postprocess")
...
elapsed = timerx.stop("postprocess")

print(elapsed)
print(timerx.summary())

Decorator

track works with or without parentheses and supports sync and async functions. It records calls that return normally and calls that raise.

@timerx.track
def sync_work():
    ...


@timerx.track()
def also_sync_work():
    ...


@timerx.track(name="fetch")
async def async_work():
    ...

Stats accumulate across calls:

sync_work()
sync_work()

stats = timerx.get_stats()
print(stats["sync_work"]["count"])
print(stats["sync_work"]["avg"])

Laps

lap records a named block. It records in finally, so exceptions are included, and nested laps work naturally.

with timerx.lap("outer"):
    with timerx.lap("inner"):
        ...

Stopwatches

Named stopwatches can run concurrently. Multiple starts with the same name are stacked, so each stop(name) closes the most recent start.

timerx.start("download")
timerx.start("parse")

timerx.stop("parse")
timerx.stop("download")

Summaries

print(timerx.summary())
print(timerx.summary(unit="ms"))

Supported units are auto, s, ms, us, and µs. Auto mode picks seconds, milliseconds, or microseconds per value.

Example output:

name           count  total    avg      min      max      last
-------------  -----  -------  -------  -------  -------  -------
preprocessing  1      5.12ms   5.12ms   5.12ms   5.12ms   5.12ms

Programmatic Stats

get_stats() returns plain dictionaries:

{
    "preprocessing": {
        "count": 1,
        "total": 0.00512,
        "min": 0.00512,
        "max": 0.00512,
        "last": 0.00512,
        "avg": 0.00512,
    }
}

Isolated Instances

Use TimerX() when building libraries or tests that should not write into timerx's global state.

from timerx import TimerX

tx = TimerX()

with tx.lap("local"):
    ...

print(tx.get_stats())

Development

pip install -e ".[test]"
pytest
python examples/demo.py

Contributing

Contributions are welcome. If you find a bug, have an API idea, or want to add coverage for an edge case, please open an issue or pull request.

License

timerx is released under the MIT License. See LICENSE for details.

Project details


Download files

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

Source Distribution

timerx-0.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

timerx-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file timerx-0.1.0.tar.gz.

File metadata

  • Download URL: timerx-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for timerx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 20ed245839d37be135494d9e72b67589c7747e2feb79f5cb4aba3138f2c9084c
MD5 2b376e3f1d0477e4acb0168c7d052d98
BLAKE2b-256 883334bf1024b9b7817c63517ffdba0007aa5ca0d1c7a5d0feb18d3d94cc14f6

See more details on using hashes here.

File details

Details for the file timerx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: timerx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for timerx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d6ba0c2cca7de7f89510055f99e692e8a2f6d2260c6697ea6ed19c9f082ca72
MD5 f03e235e03e52a86dd68bd9eaa0988ce
BLAKE2b-256 f761e470f73dab403f16a79b4e8bdd0ccf9943446c74ce356235e1448f21b9ee

See more details on using hashes here.

Supported by

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