Created
August 21, 2017 06:20
-
-
Save AntoineToubhans/a66fa2d1518479695a227f16adef62db 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 time | |
from functools import wraps | |
_total_time_call_stack = [0] | |
def better_time_tracker(log_fun): | |
def _better_time_tracker(fn): | |
@wraps(fn) | |
def wrapped_fn(*args, **kwargs): | |
global _total_time_call_stack | |
_total_time_call_stack.append(0) | |
start_time = time.time() | |
try: | |
result = fn(*args, **kwargs) | |
finally: | |
elapsed_time = time.time() - start_time | |
inner_total_time = _total_time_call_stack.pop() | |
partial_time = elapsed_time - inner_total_time | |
_total_time_call_stack[-1] += elapsed_time | |
# log the result | |
log_fun({ | |
'function_name': fn.__name__, | |
'total_time': elapsed_time, | |
'partial_time': partial_time, | |
}) | |
return result | |
return wrapped_fn | |
return _better_time_tracker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment