Skip to content

Instantly share code, notes, and snippets.

@hwayne
Created June 8, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwayne/034888346f58968db7a7df22f1c90ad8 to your computer and use it in GitHub Desktop.
Save hwayne/034888346f58968db7a7df22f1c90ad8 to your computer and use it in GitHub Desktop.
Showing difference in using lru_cache
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