Context manager that prints the wall-clock duration of a labelled phase. Usage:: with timed("model load"): model = load_model(...)
(label: str)
| 708 | |
| 709 | @contextlib.contextmanager |
| 710 | def timed(label: str): |
| 711 | """Context manager that prints the wall-clock duration of a labelled phase. |
| 712 | |
| 713 | Usage:: |
| 714 | |
| 715 | with timed("model load"): |
| 716 | model = load_model(...) |
| 717 | """ |
| 718 | print(f"[timing] {label} — starting", flush=True) |
| 719 | t0 = time.perf_counter() |
| 720 | try: |
| 721 | yield |
| 722 | finally: |
| 723 | elapsed = time.perf_counter() - t0 |
| 724 | print(f"[timing] {label} — done in {elapsed:.1f}s", flush=True) |
no outgoing calls