Last active
December 1, 2021 21:43
-
-
Save xwjiang2010/4727f64f5a8352561ffd1fd12eab34fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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