A simple context manager for timing blocks of Python code.
Project description
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.
startis simply adatetime.datetimeobject representing (approximately) when the context started.endis simply adatetime.datetimeobject representing (approximately) when the context ended.durationis 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_nsis 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.Profilecan 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file timer_context-1.1.1.tar.gz.
File metadata
- Download URL: timer_context-1.1.1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a1863e52cc768c2008ee0f2353c39ea5cbdaf46f6a2ea747551c4efc8e04336
|
|
| MD5 |
bd9656ebe0f9d8a8a52e3c623993393b
|
|
| BLAKE2b-256 |
d04fbb7d9b09ff45df2b81a45095c0a5b73cfe58d56973b84af4e9284f67be0d
|
Provenance
The following attestation bundles were made for timer_context-1.1.1.tar.gz:
Publisher:
semantic-release.yaml on qthequartermasterman/TimerContext
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
timer_context-1.1.1.tar.gz -
Subject digest:
8a1863e52cc768c2008ee0f2353c39ea5cbdaf46f6a2ea747551c4efc8e04336 - Sigstore transparency entry: 171361485
- Sigstore integration time:
-
Permalink:
qthequartermasterman/TimerContext@8d242d4d0a5f5e95d357257fe0937d43340813fb -
Branch / Tag:
refs/heads/main - Owner: https://github.com/qthequartermasterman
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
semantic-release.yaml@8d242d4d0a5f5e95d357257fe0937d43340813fb -
Trigger Event:
push
-
Statement type:
File details
Details for the file timer_context-1.1.1-py3-none-any.whl.
File metadata
- Download URL: timer_context-1.1.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8d014a897fa9c6b9c55c713ec04a1561b8314621357a389d90950afa742e104
|
|
| MD5 |
b660cd68979ea6815080363ca1836268
|
|
| BLAKE2b-256 |
db5d71c378d1ce5ab5abec88effb2b5d2c8abb56799eb07a5b83a2fdc024eee4
|
Provenance
The following attestation bundles were made for timer_context-1.1.1-py3-none-any.whl:
Publisher:
semantic-release.yaml on qthequartermasterman/TimerContext
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
timer_context-1.1.1-py3-none-any.whl -
Subject digest:
f8d014a897fa9c6b9c55c713ec04a1561b8314621357a389d90950afa742e104 - Sigstore transparency entry: 171361486
- Sigstore integration time:
-
Permalink:
qthequartermasterman/TimerContext@8d242d4d0a5f5e95d357257fe0937d43340813fb -
Branch / Tag:
refs/heads/main - Owner: https://github.com/qthequartermasterman
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
semantic-release.yaml@8d242d4d0a5f5e95d357257fe0937d43340813fb -
Trigger Event:
push
-
Statement type: