python-timer-context-manager.py
import time
from contextlib import contextmanager
@contextmanager
def timer(label="block"):
start = time.perf_counter()
try:
yield
finally:
print(f"{label}: {(time.perf_counter() - start) * 1000:.1f} ms")
with timer("sum of squares"):
total = sum(n * n for n in range(1_000_000))
print(total)