Created
March 2, 2020 06:25
-
-
Save mcihad/533ade50741c74740e6a2596283a3622 to your computer and use it in GitHub Desktop.
python lru_cache performance test. lru_cache performans testi
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 datetime import datetime | |
from functools import lru_cache, wraps | |
""" | |
eğer fonksiyon aldığı parametreye göre aynı sonucu döndürüyor ve bu çok sık tekrar ediyorsa | |
lru_cache(Least Recently Used) kullanmak performansı ciddi oranda artıracaktır. | |
""" | |
def profiler(num: int): | |
def decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
start = datetime.now() | |
for i in range(num): | |
func(*args, **kwargs) | |
end = datetime.now() | |
print(end - start) | |
return wrapper | |
return decorator | |
@profiler(1000) | |
def total_normal(n: int) -> int: | |
t = 0 | |
for i in range(n): | |
t += i | |
return t | |
@profiler(1000) | |
@lru_cache(maxsize=100) | |
def total_cache(n: int) -> int: | |
t = 0 | |
for i in range(n): | |
t += i | |
return t | |
if __name__ == '__main__': | |
total_normal(100000) #0:00:06.160528 | |
total_cache(100000) #0:00:00.006492 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment