| """ | |
| Timing utility — context manager and decorator for measuring latency. | |
| """ | |
| from __future__ import annotations | |
| import time | |
| from contextlib import contextmanager | |
| from typing import Iterator | |
| 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) | |