Skip to content

Instantly share code, notes, and snippets.

@carsongee
Last active December 10, 2020 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carsongee/31221079718d048934685d2e1bf7e56d to your computer and use it in GitHub Desktop.
Save carsongee/31221079718d048934685d2e1bf7e56d to your computer and use it in GitHub Desktop.
Quick & dirty python timer for copy/paste perf. Handy for quick measurements of functions with the decorator or blocks with the context manager
from contextlib import contextmanager
from functools import wraps
from time import perf_counter
@contextmanager
def t(name: str):
start = perf_counter()
yield
print(f'TIMER {name}: {perf_counter() - start}')
def td(f):
@wraps(f)
def wrapper(*args, **kwargs):
with t(f'{f.__name__}'):
return f(*args, **kwargs)
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment