Created
December 16, 2019 01:50
-
-
Save FerdinaKusumah/85ea470a20f8d582cdb3deaeb12bb007 to your computer and use it in GitHub Desktop.
Memoization Fibonacci Lru cache
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
import timeit | |
from functools import lru_cache | |
@lru_cache(maxsize=256) | |
def fibonacci_lru_cache(n: int): | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 | |
return fibonacci_lru_cache(n-1) + fibonacci_lru_cache(n-2) | |
print(timeit.timeit("fibonacci_lru_cache(N)", globals=globals(), number=1)) | |
# 2.894699999966832e-05 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment