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 codetimingVerified import paths — ran on the pinned version, not inferred.
Demonstrates `codetiming.Timer` used as a class, a context manager, and a decorator. It also shows how to access accumulated statistics for named timers.
Upgrade to Python 3.7+ or pin `codetiming<1.4.0` in your dependencies.
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.
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')`.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']`.
Install the library using pip: `pip install codetiming`
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.
When using `Timer` as a decorator, always call it: `@Timer()` or `@Timer(name='my_func')`.
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.
No dependency data recorded yet.