Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ycui1
Last active September 2, 2020 09:18
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 ycui1/e0cc4cd38dc252dd8b7a00965263f903 to your computer and use it in GitHub Desktop.
Save ycui1/e0cc4cd38dc252dd8b7a00965263f903 to your computer and use it in GitHub Desktop.
>>> import time
... def logging_time(func):
... """Decorator that logs time"""
... def logger(*args, **kwargs):
... """Function that logs time"""
... start = time.time()
... func(*args, **kwargs)
... print(f"Calling {func.__name__}: {time.time() - start:.5f}")
...
... return logger
...
... @logging_time
... def calculate_sum_n(n):
... return sum(range(n))
...
... @logging_time
... def say_hi(whom, greeting="Hello"):
... print(f"{greeting}, {whom}!")
...
... calculate_sum_n(100000)
... say_hi("John", greeting="Hi")
...
Calling calculate_sum_n: 0.00187
Hi, John!
Calling say_hi: 0.00001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment