Timer Context Manager

A Python context manager that times how long a block of code takes to run. Helpful for measuring performance of scripts or functions.

Python7/8/2025
#python#performance
Python
import time

from contextlib import contextmanager

@contextmanager
def timer(name):
    start = time.time()
    yield
    end = time.time()
    print(f"{name}: {end - start:.2f}s")
...
Loading comments...