Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created May 17, 2023 11:44
Show Gist options
  • Save bbelderbos/d23b350b93ba1e14065a7c3cfba07d5a to your computer and use it in GitHub Desktop.
Save bbelderbos/d23b350b93ba1e14065a7c3cfba07d5a to your computer and use it in GitHub Desktop.
from functools import wraps
from time import time, sleep
def timing(f):
"""A simple timer decorator"""
@wraps(f)
def wrapper(*args, **kwargs):
start = time()
result = f(*args, **kwargs)
end = time()
print(f'Elapsed time {f.__name__}({args}): {end - start}')
return result
return wrapper
cache = {}
@timing
def func(*args) -> int:
if args in cache:
print("retrieve cache")
return cache[args]
sleep(2)
result = sum(args)
print("store cache")
cache[args] = result
return result
if __name__ == "__main__":
print(func(1,2,3))
print(func(1,2,3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment