Skip to content

Instantly share code, notes, and snippets.

@MagnetonBora
Created December 6, 2019 20:18
Show Gist options
  • Save MagnetonBora/90adaa1f9158ee74868b627c7d1134b2 to your computer and use it in GitHub Desktop.
Save MagnetonBora/90adaa1f9158ee74868b627c7d1134b2 to your computer and use it in GitHub Desktop.
# This program will demonstrate how we can use python decorators
# to measure the time of function invoking
import time
def chrono(fn):
def wrapper(*args):
start = time.time() # get current time before running the function
result = fn(*args) # invoking function
duration = time.time() - start # measuring the duration
print(f'The total time for function invoking is {duration} seconds')
return result
return wrapper
@chrono
def run(timeout=5):
print(f'Invoking function run with timeout {timeout} second')
time.sleep(timeout)
def main():
run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment