Skip to content

Instantly share code, notes, and snippets.

@polyrand
Last active September 8, 2023 18:09
Show Gist options
  • Save polyrand/bb39fb93246ced7464abf52d87fec3a7 to your computer and use it in GitHub Desktop.
Save polyrand/bb39fb93246ced7464abf52d87fec3a7 to your computer and use it in GitHub Desktop.
import contextlib
from functools import wraps
import cProfile
from typing import Optional
import io
import pstats
import time
from pathlib import Path
from functools import wraps
@contextlib.contextmanager
def profile(filename: Path, *args, **kwargs):
profile = cProfile.Profile(*args, **kwargs)
profile.enable()
yield
profile.disable()
s = io.StringIO()
sortby = pstats.SortKey.CUMULATIVE
ps = pstats.Stats(profile, stream=s).strip_dirs().sort_stats(sortby)
ps.print_stats()
with open(filename.with_suffix(".txt"), "w") as f:
f.write(s.getvalue())
profile.dump_stats(filename.with_suffix(".prof"))
def profiled(name: Path):
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
with profile(name):
return fn(*args, **kwargs)
return wrapper
return decorator
@contextlib.contextmanager
def timeit(output_file: Optional[Path] = None):
start = time.perf_counter()
yield
end = time.perf_counter()
if output_file is not None:
with open(output_file, "a") as f:
f.write(f"{end - start}\n")
else:
print(f"{end - start}")
def timed(output_file: Optional[Path] = None):
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
with timeit(output_file):
return fn(*args, **kwargs)
return wrapper
return decorator
# context manager
def my_function():
with profile(Path("/home/ubuntu/profiles/prof")):
return 1
# decorator
@profiled(Path("/home/ubuntu/profiles/prof"))
def my_function():
return 1
# context manager
def my_function():
with timeit(Path("/home/ubuntu/profiles/execution-times.txt")):
return 1
# decorator
@timed(Path("/home/ubuntu/profiles/execution-times.txt"))
def my_function():
return 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment