Skip to content

Instantly share code, notes, and snippets.

@richiefrost
Last active May 29, 2020 21:49
Show Gist options
  • Save richiefrost/b5f5159ea2011bdfc33a138a1e940cca to your computer and use it in GitHub Desktop.
Save richiefrost/b5f5159ea2011bdfc33a138a1e940cca to your computer and use it in GitHub Desktop.
from time import time
def log_time(func):
"""Logs the time it took for func to execute"""
def wrapper(*args, **kwargs):
start = time()
val = func(*args, **kwargs)
end = time()
duration = end - start
print(f'{func.__name__} took {duration} seconds to run')
return val
return wrapper
# Example usage
@log_time
def get_data(db, query):
"""Gets data from a SQL-based database"""
data = db.get(query)
return data
if __name__ == '__main__':
# Decorated function will print 'get_data took X seconds to run'
db = SQLDB()
query = 'SELECT * FROM foo'
data = get_data(db, query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment