Skip to content

Instantly share code, notes, and snippets.

@arpitbbhayani
Created February 7, 2020 06:28
Show Gist options
  • Save arpitbbhayani/9fca21f955fa896bbbd592cf10a32eb7 to your computer and use it in GitHub Desktop.
Save arpitbbhayani/9fca21f955fa896bbbd592cf10a32eb7 to your computer and use it in GitHub Desktop.
import time
def my_decorator(fn):
"""my_decorator is a custom decorator that wraps any function
and prints on stdout the time for execution.
"""
def wrapper_function(*args, **kwargs):
start_time = time.time()
# invoking the wrapped function and getting the return value.
value = fn(*args, **kwargs)
print("the function execution took:", time.time() - start_time, "seconds")
# returning the value got after invoking the wrapped function
return value
return wrapper_function
@my_decorator
def area(l, b):
return l * b
>>> area(3, 4)
the function execution took: 9.5367431640625e-07 seconds
12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment