File size: 563 Bytes
dbc3c35 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from collections.abc import Iterator
from contextlib import contextmanager
from time import perf_counter
class TimingLog:
def __init__(self) -> None:
self._steps: dict[str, float] = {}
@contextmanager
def measure(self, name: str) -> Iterator[None]:
started = perf_counter()
try:
yield
finally:
self._steps[name] = round(perf_counter() - started, 3)
def to_dict(self) -> dict[str, float]:
total = round(sum(self._steps.values()), 3)
return {**self._steps, "total": total}
|