Skip to main content

TimerContext

Context Manager for timing blocks of python code simply.

Installation

Install TimerContext using pip, uv, or another package manager by downloading from PyPI.

pip install timer-context
uv pip install timer-context

Usage

TimerContext is a context manager, and will time code used within a with block.

import time


def func(x):
    time.sleep(x)

with TimerContext() as timer:
    func(3)
    timer.duration  # duration will be approximately 3 seconds
    func(2)
    timer.duration  # duration will be approximately 5 seconds
    func(1)
    timer.duration  # duration will be approximately 6 seconds

print(timer.start)
print(timer.end)
print(timer.duration)  # duration will be approximately 6 seconds

Any code inside of the context manager will contribute to the duration. As soon as the context is left, the timer will stop counting.

API Reference

TimerContext has four attributes intended to be used by the user, start, end, duration, and duration_ns.

  • start is simply a datetime.datetime object representing (approximately) when the context started.
  • end is simply a datetime.datetime object representing (approximately) when the context ended.
  • duration is a property representing the number of milliseconds of the duration of the timer (as a float). If the context is still ongoing, it will report the number of milliseconds since the timer context until duration was called. If the context is closed, then it will report the number of milliseconds the context was open.
  • duration_ns is a property representing the number of nanoseconds of the duration of the timer (as an int).

Notice on Resolution

The duration is always calculated using time.perf_counter_ns and then converted to milliseconds as a float. The precision of this timer is limited by the resolution of the performance counter on your particular environment.

The precision is also limited by the overhead of calling a context manager. If your context is exceptionally fast (on the order of single nanoseconds), this overhead is non-negligible, and you would be better served by calling time.perf_counter_ns directly. For any block that runs for longer than that, the overhead is proportionally small/negligible.

Because the counter used for duration is set after defining timer.start and before defining timer.end, timer.duration might not exactly equal timer.end-timer.start, generally. The times used for start and end are lower resolution than the performance counter for the duration. Thus, they should generally only be used for reference, and not for calculating a duration.

Comparison with timeit and cProfile.

Timing/profiling Python code is generally done with the timeit and/or cProfile modules in the standard library. These are both great tools, and may be better suited for your task.

You should use timeit if:

  • You need statistics on runtime by running an identical code snippet many times.

You should use cProfile if:

  • You need more detailed reports on the runtime of individual lines/function calls. (cProfile.Profile can also conveniently be used as a context manager!)

You should use TimerContext if:

  • You need to only time a block of code once without interrupting its flow.
  • You do not need detailed profiling reports
  • Rewriting your block of code as a function would be inconvenient.
  • You want an incredibly simple interface for timing code.

Contributing

Please open an Issue if you find a bug or would like to request a feature. Bugs are generally fixed promptly. Because of TimerContext's goal of being a radically simple interface, complex feature requests will likely be closed, but genuine improvements that do not sacrifice the vision of simplicity may be considered. We recommend opening an issue to discuss new features before opening a Pull Request. Pull Requests for bug fixes are always welcome.

Metadata

Release files for timer-context 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timer-context 1.1.1
File Size Uploaded
timer_context-1.1.1.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for timer-context 1.1.1
File Interpreter ABI Platform
timer_context-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / timer_context-1.1.1.tar.gz

Download URL timer_context-1.1.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8a1863e52cc768c2008ee0f2353c39ea5cbdaf46f6a2ea747551c4efc8e04336
BLAKE2b-256 checksum
How to use checksums
d04fbb7d9b09ff45df2b81a45095c0a5b73cfe58d56973b84af4e9284f67be0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

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 Feb 14, 2025.

Transparency log

Release files / timer_context-1.1.1-py3-none-any.whl

Download URL timer_context-1.1.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8d014a897fa9c6b9c55c713ec04a1561b8314621357a389d90950afa742e104
BLAKE2b-256 checksum
How to use checksums
db5d71c378d1ce5ab5abec88effb2b5d2c8abb56799eb07a5b83a2fdc024eee4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

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 Feb 14, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page