Registry / observability / codetiming

codetiming

JSON →
library1.4.0pypypi✓ verified 25d ago

Codetiming is a flexible and customizable Python library for timing code execution. It allows users to measure the performance of different code blocks, functions, or entire scripts using a simple `Timer` class, context manager, or decorator. The current version is 1.4.0, and releases are made on an as-needed basis.

pip install codetiming
INSTALL
IMPORT
SIG · CODETIMING
C
codetiming
observabilitypythonv1.4.0
Install
1.6s avg
Import
65ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.066s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.064s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Timer
from codetiming import Timer
from timer import Timer
While tutorials might show `from timer import Timer`, the installed package is `codetiming`, requiring import from `codetiming`.

Demonstrates `codetiming.Timer` used as a class, a context manager, and a decorator. It also shows how to access accumulated statistics for named timers.

from codetiming import Timer import time # As a class t = Timer(name="class method") t.start() time.sleep(0.01) t.stop() # As a context manager with Timer(name="context manager"): time.sleep(0.02) # As a decorator @Timer(name="decorator") def my_function(): time.sleep(0.03) my_function() # Accessing accumulated times for named timers print(f"Total time for 'class method': {Timer.timers.total('class method'):.4f} seconds") print(f"All named timers: {Timer.timers.items()}")
Debug
Known issues
breakingExplicit support for Python 3.6 was dropped in version 1.4.0. Users on Python 3.6 should pin their `codetiming` dependency to `<1.4.0`.
fix
Upgrade to Python 3.7+ or pin `codetiming<1.4.0` in your dependencies.
affects: >=1.4.0
gotchaWhen using `Timer` as a decorator on `async` functions, the reported time might be 0 or incorrect. This is a known open issue.
fix
For `async` functions, prefer using `Timer` as a class or context manager explicitly measuring `await` calls, or check the GitHub issues for updates on fix.
affects: All versions
gotchaThe `text` argument for `Timer` is not an f-string. If you want to include f-string-like formatting (e.g., a variable from the current scope) within the `text` string, you must use double curly braces to escape the parts that `codetiming` will format.
fix
Use double curly braces for `codetiming`'s placeholders if mixing with f-string formatting, e.g., `Timer(text=f'Task {task_name} finished in {{:.4f}} seconds')`.
affects: All versions
gotchaNamed timers accumulate elapsed time across multiple uses. If you reuse a `Timer` instance or a name for `Timer` and expect a fresh measurement, you need to manually reset or manage these timings (e.g., retrieve statistics and then delete the timer from `Timer.timers`).
fix
Be aware of the accumulation behavior of `Timer.timers`. If single measurements are needed for a named timer, create a new `Timer` instance or delete the existing named timer from `Timer.timers` before reuse. Example: `del Timer.timers['my_timer_name']`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'codetiming'
The 'codetiming' library has not been installed in your Python environment or the environment where your script is being run.
fix
Install the library using pip: `pip install codetiming`
TimerError: Timer is running. Use .stop() to stop it
You attempted to call the `.start()` method on a `Timer` object that has already been started and has not yet been stopped.
fix
Ensure that `.stop()` is called before `.start()` is called again on the same `Timer` instance, or use a new `Timer` instance for each timing interval. When using the context manager (`with Timer():`), `start` and `stop` are handled automatically.
TypeError: 'Timer' object is not callable
This error often occurs when trying to use `codetiming.Timer` as a decorator without instantiating it, e.g., `@Timer` instead of `@Timer()`. The decorator must be called to create a Timer instance.
fix
When using `Timer` as a decorator, always call it: `@Timer()` or `@Timer(name='my_func')`.
AttributeError: 'Timer' object has no attribute 'start'
This typically happens if you try to manually call `.start()` or `.stop()` on a `Timer` object that is being used as a context manager, as its `__enter__` and `__exit__` methods handle starting and stopping automatically.
fix
When using `Timer` as a context manager (`with Timer():`), do not manually call `.start()` or `.stop()`. The timing automatically begins upon entering the `with` block and stops upon exiting.
Upgrade
Version history
1.4.0latest on PyPI · released Nov 8, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
1
Resources
codetiming — pip install codetiming · libregistry