File size: 532 Bytes
bbc3fdf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | """
Timing utility — context manager and decorator for measuring latency.
"""
from __future__ import annotations
import time
from contextlib import contextmanager
from typing import Iterator
@contextmanager
def timed(label: str = "") -> Iterator[dict]:
"""Context manager that yields a dict and fills it with elapsed ms."""
info: dict = {"label": label, "elapsed_ms": 0.0}
t0 = time.perf_counter()
try:
yield info
finally:
info["elapsed_ms"] = round((time.perf_counter() - t0) * 1000.0, 3)
|