Created
May 17, 2023 11:44
-
-
Save bbelderbos/d23b350b93ba1e14065a7c3cfba07d5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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