Skip to content

About

Hierarchical execution timing for Python: nested sections, categories, asyncio and threads.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

executiontimer

PyPI version Python versions CI License Documentation

Time named sections of your code with a with block or a decorator. Sections nested inside each other form a tree, so you see where the time actually went, not just one number at the end.

Zero dependencies. Fully typed. Works with threads and asyncio.

Documentation

seba2390.github.io/ExecutionTimer

Installation

pip install executiontimer

Requires Python 3.11+. The install name is executiontimer; the import name is execution_timer.

Quick start

import time

from execution_timer import TimerContext, get_execution_times_report

with TimerContext("load_data"):
    time.sleep(0.12)

with TimerContext("solve"):
    for i in range(3):
        with TimerContext("step", category="gpu", counter=i):
            time.sleep(0.05)
    with TimerContext("postprocess", category="cpu"):
        time.sleep(0.03)

print(get_execution_times_report())
Total time: 312.838 ms.

load_data: 124.682 ms (39.85%)
solve: 188.157 ms (60.15%)
..  step: 154.718 ms (49.46%)
..  postprocess: 33.295 ms (10.64%)

TimerContext also works as a decorator, including on async def functions:

@TimerContext("preprocess")
def preprocess(rows: list[str]) -> list[str]:
    return [row.strip() for row in rows]

Learn more

License

MIT — see LICENSE.

About

Hierarchical execution timing for Python: nested sections, categories, asyncio and threads.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages