contexttimer
raw JSON → 0.3.3 verified Fri May 01 auth: no python maintenance
A timer context manager measuring the clock wall time of the code block it contains. Version 0.3.3 is the latest, with no recent updates (last release in 2018). The library provides a simple context manager and decorator for timing code blocks, defaulting to wall-clock time. It supports alternative timers via Python's time module.
pip install contexttimer Common errors
error AttributeError: module 'contexttimer' has no attribute 'Timer' ↓
cause Incorrect import: using 'import contexttimer' and then 'contexttimer.Timer' but Timer is not a top-level attribute.
fix
Use 'from contexttimer import Timer'
error NameError: name 'timer' is not defined ↓
cause Trying to use the decorator without importing it properly.
fix
Use 'from contexttimer import timer' and then apply @timer
Warnings
gotcha The default timer is time.perf_counter (Python 3) or time.clock (Python 2). On Python 3.7+, time.clock is removed, but the library handles it. Still, avoid relying on time.clock. ↓
fix Use a newer library like 'timer' or 'timeit' if you need cross-platform consistency.
deprecated The library is no longer actively maintained (last release 2018). It may have issues with newer Python versions (3.10+). ↓
fix Consider using 'timer' (pip install timer) or 'timeit' from stdlib.
Imports
- Timer wrong
import contexttimer; Timercorrectfrom contexttimer import Timer - timer
from contexttimer import timer
Quickstart
from contexttimer import Timer
import time
with Timer() as t:
time.sleep(1)
print(f"Elapsed: {t.elapsed}")