Skip to content

Instantly share code, notes, and snippets.

@xwjiang2010
Last active December 1, 2021 21:43
Show Gist options
  • Save xwjiang2010/4727f64f5a8352561ffd1fd12eab34fc to your computer and use it in GitHub Desktop.
Save xwjiang2010/4727f64f5a8352561ffd1fd12eab34fc to your computer and use it in GitHub Desktop.
import functools
import time
_enabled = True
_timing = dict()
def enable_record_timing():
global _enabled
_enabled = True
def record_timing(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if _enabled:
start = time.time()
result = func(*args, **kwargs)
total = _timing.get(func.__name__, 0)
total += (time.time() - start)
_timing[func.__name__] = total
return result
else:
return func(*args, **kwargs)
return wrapper
def print_timing():
if _enabled:
[print(f"key: {k}, time: {v}") for k, v in _timing.items()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment