Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
Created May 2, 2020 09:18
Show Gist options
  • Select an option

  • Save jonathanagustin/7a3464d2f2ef8a158f49bdc188504dc9 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanagustin/7a3464d2f2ef8a158f49bdc188504dc9 to your computer and use it in GitHub Desktop.
Python - time a function with a decorator
from functools import wraps
from time import time
def measure(func):
@wraps(func)
def _time_it(*args, **kwargs):
start = int(round(time() * 1000))
try:
return func(*args, **kwargs)
finally:
end_ = int(round(time() * 1000)) - start
print(f"Total execution time: {end_ if end_ > 0 else 0} ms")
return _time_it
@measure
def hello():
print('hello world')
hello()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment