Time a block of code
PythonA context manager that prints how long its body took, without scattering time.perf_counter calls through the function.
import time
from contextlib import contextmanager
@contextmanager
def timed(label):
start = time.perf_counter()
try:
yield
finally:
print(f'{label}: {time.perf_counter() - start:.3f}s')
with timed('import'):
load_everything()