Skip to content

Instantly share code, notes, and snippets.

@danemacaulay
Last active April 16, 2019 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danemacaulay/be56437c47c02c98108594385c742c92 to your computer and use it in GitHub Desktop.
Save danemacaulay/be56437c47c02c98108594385c742c92 to your computer and use it in GitHub Desktop.
from functools import wraps
import time
import inspect
def timing(f):
@wraps(f)
def wrapper(*args, **kwargs):
module = inspect.getmodule(f).__name__
start = time.time()
result = f(*args, **kwargs)
end = time.time()
elapsed = str(round(end - start, 4))
print('Elapsed time: {}.{} {} seconds'.format(module, f.__name__, elapsed))
return result
return wrapper
@danemacaulay
Copy link
Author

danemacaulay commented Apr 16, 2019


@timing
def do_calc():
  time.sleep(1)
  print('slept')

do_calc()
# slept
# Elapsed time: __main__.do_calc 1.0004 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment