Skip to content

Instantly share code, notes, and snippets.

@RuRo
Created December 24, 2018 12:58
Show Gist options
  • Save RuRo/5278362923870db2ab2c387d02b36512 to your computer and use it in GitHub Desktop.
Save RuRo/5278362923870db2ab2c387d02b36512 to your computer and use it in GitHub Desktop.
import time
import functools
timers = {}
def timed(func):
global timers
timers[func] = {"T":0, "N":0}
@functools.wraps(func)
def wrapped(*args, **kwargs):
T = time.perf_counter()
res = func(*args, **kwargs)
T = time.perf_counter() - T
timers[func]["T"] += T
timers[func]["N"] += 1
return res
return wrapped
@timed
def my_functions(some, arguments, also, kwarguments=None):
pass
if __name__ == '__main__':
my_functions(1, 2, 3)
print("\n".join("Time spent in {:>24}: {} sec over {} iterations".format(func.__name__, T['T'], T['N'])
for func, T in timers.items()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment