Skip to content

Instantly share code, notes, and snippets.

@cgravier
Created March 6, 2021 09:15
Show Gist options
  • Save cgravier/8e9c1903ada68cddf065dc36f657432d to your computer and use it in GitHub Desktop.
Save cgravier/8e9c1903ada68cddf065dc36f657432d to your computer and use it in GitHub Desktop.
import logging
import time
from functools import wraps
logger = logging.getLogger(__name__)
# Misc logger setup so a debug log statement gets printed on stdout.
logger.setLevel("DEBUG")
handler = logging.StreamHandler()
log_format = "%(asctime)s %(levelname)s -- %(message)s"
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
logger.addHandler(handler)
def timed(func):
"""This decorator prints the execution time for the decorated function."""
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
logger.debug("{} ran in {}s".format(func.__name__, round(end - start, 2)))
return result
return wrapper
@timed
def some_function_timed():
print("Hello world")
if __name__ == "__main__":
slow_function()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment