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.
seba2390.github.io/ExecutionTimer
pip install executiontimerRequires Python 3.11+. The install name is executiontimer; the import name is
execution_timer.
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]- Getting started
- User guide: timing code, counters, categories, reports and JSON export, threads, asyncio and generators
- API reference
- Changelog and contributing guide
MIT — see LICENSE.