from functools import lru_cache, partial
import timeit
@lru_cache(maxsize = 128)
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
#the first time
execution_time = timeit.Timer(partial(fibonacci, 50)).timeit(1)
print(execution_time)
#the second time
execution_time = timeit.Timer(partial(fibonacci, 50 )).timeit(1)
print(execution_time)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter