Created
June 8, 2016 22:38
-
-
Save hwayne/034888346f58968db7a7df22f1c90ad8 to your computer and use it in GitHub Desktop.
Showing difference in using lru_cache
This file contains 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 functools | |
import timeit | |
def fib(x): | |
if x <= 1: | |
return 1 | |
return fib(x-1) + fib(x-2) | |
@functools.lru_cache() | |
def cached_fib(x): | |
if x in (0,1): | |
return 1 | |
return cached_fib(x-1) + cached_fib(x-2) | |
print(timeit.timeit("fib(20)", globals=globals(), number=1000)) | |
print(timeit.timeit("cached_fib(20)", globals=globals(), number=1000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment