Skip to main content

TimeRun is a Python library for time measurements.

Project description

TimeRun

TimeRunPython package for time measurement.

Version Status License Coverage Total Downloads

TimeRun is a single-file Python package with no dependencies beyond the standard library. It records wall-clock time and CPU time for code blocks or function calls and supports optional metadata (e.g. run id, tags) per measurement.

For the full value proposition and positioning, see Why TimeRun on the docs site.

Installation

From PyPI:

pip install timerun

From source:

pip install git+https://github.com/HH-MWB/timerun.git

Note: Requires Python 3.10+.

Usage

Time Code Block

Use with Timer() as m: (or async with). The yielded Measurement has wall_time and cpu_time:

>>> from timerun import Timer
>>> with Timer() as m:
...     pass  # code block to be measured
...
>>> m.wall_time.timedelta
datetime.timedelta(microseconds=11)
>>> m.cpu_time.timedelta
datetime.timedelta(microseconds=8)

Note: On block exit the timer records CPU time first, then wall time, so wall time is slightly larger than CPU time even when there is no I/O or scheduling.

Time Function Calls

Use @Timer(). One Measurement per call is appended to the callable’s measurements deque:

>>> from timerun import Timer
>>> @Timer()
... def func():  # function to be measured
...     return
...
>>> func()
>>> func.measurements[-1].wall_time.timedelta
datetime.timedelta(microseconds=11)
>>> func.measurements[-1].cpu_time.timedelta
datetime.timedelta(microseconds=8)

Note: Argument maxlen caps how many measurements are kept (e.g. @Timer(maxlen=10)). By default the deque is unbounded.

Callbacks on Start and End

Optional on_start and on_end callbacks run once per measurement. Both receive the measurement instance (on_start before timings are set, on_end after). Typical uses are logging, forwarding to OpenTelemetry, or enqueueing to a metrics pipeline.

>>> from timerun import Timer
>>> with Timer(on_end=lambda m: print(m.wall_time.timedelta)):
...     pass  # code block to be measured
...  
0:00:00.000008

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.

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

timerun-0.6.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

timerun-0.6.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file timerun-0.6.1.tar.gz.

File metadata

  • Download URL: timerun-0.6.1.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for timerun-0.6.1.tar.gz
Algorithm Hash digest
SHA256 36ec23b48b8cd8cbdb83349407ff2727db124901ec1079b194713b9a0aed63ea
MD5 1434c91e34b9871746f78ba5d977dfad
BLAKE2b-256 48672e08d42b7ed762c9bd58f9b42877d80a2d2aad8139718ecf1191ceb5f702

See more details on using hashes here.

Provenance

The following attestation bundles were made for timerun-0.6.1.tar.gz:

Publisher: release.yaml on HH-MWB/timerun

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file timerun-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: timerun-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for timerun-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6529ac06d61583727ec2b0b4b6cdaaef30b7e68778dbe5b92c263c0a299d0718
MD5 f45df50325416506852d7691202da69d
BLAKE2b-256 d9396856d6535af2dee640df84eae91985144e5772c84e690f62b511a8c4beeb

See more details on using hashes here.

Provenance

The following attestation bundles were made for timerun-0.6.1-py3-none-any.whl:

Publisher: release.yaml on HH-MWB/timerun

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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