import cProfile import pstats from pathlib import Path from typing import Callable def profile(func: Callable, output: Path) -> None: profiler = cProfile.Profile() profiler.enable() func() profiler.disable() output.parent.mkdir(parents=True, exist_ok=True) stats = pstats.Stats(profiler) stats.dump_stats(str(output))