Skip to content

Instantly share code, notes, and snippets.

@grihabor
Last active November 1, 2017 07:37
Show Gist options
  • Save grihabor/cf414172d425398d26e11f22c6bd0352 to your computer and use it in GitHub Desktop.
Save grihabor/cf414172d425398d26e11f22c6bd0352 to your computer and use it in GitHub Desktop.
Print function execution time if decorated #practical
from datetime import datetime
import functools
def track_time(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
start = datetime.now()
result = f(*args, **kwargs)
print('Run {}: {}'.format(f.__name__, datetime.now() - start))
return result
return wrapper
@track_time
def bad_sum(n):
s, i = 0, 0
while i <= n:
s += i
i += 1
return s
@track_time
def good_sum(n):
return sum(range(n + 1))
n = 10 ** 6
print(good_sum(n) == bad_sum(n))
#|Run good_sum: 0:00:00.327170
#|Run bad_sum: 0:00:00.874342
#|True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment