Skip to content

Instantly share code, notes, and snippets.

@chengzhongkai
Forked from bradmontgomery/LICENSE.txt
Created February 1, 2022 22:47
Show Gist options
  • Save chengzhongkai/71cb15166accda6d38478e74bb899f2a to your computer and use it in GitHub Desktop.
Save chengzhongkai/71cb15166accda6d38478e74bb899f2a to your computer and use it in GitHub Desktop.
A python decorator that logs execution time.
"""
A simple execution time logger implemented as a python decorator.
Available under the terms of the MIT license.
"""
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 slow_function():
"""This is a slow-running function used as an example."""
print("running a slow function...", end="")
time.sleep(3.2)
print("done")
if __name__ == "__main__":
slow_function()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment