This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
python3:lru_cache [2018/11/03 16:38] jguerin |
python3:lru_cache [2019/05/07 12:43] (current) jguerin |
||
---|---|---|---|
Line 4: | Line 4: | ||
===== The Fibonacci Sequence ===== | ===== The Fibonacci Sequence ===== | ||
- | The [[https:// | + | The [[https:// |
+ | |||
+ | The naive implementation is exponential. The second example using the LRU cache shows little growth up to moderately large values of //n//. | ||
<file python fibonacci.py> | <file python fibonacci.py> | ||
def fib(n): | def fib(n): | ||
Line 27: | Line 30: | ||
</ | </ | ||
+ | ==== Benchmarks ==== | ||
+ | The naive recursive implementation fails quickly as expected (at nearly a minute for //n=//40). | ||
+ | ^ %%n%% ^ fibonacci.py((Python 3.5.2)) | ||
+ | ^ 10 | .016s | .0224s | ||
+ | ^ 20 | .0216s | ||
+ | ^ 30 | .4664s | ||
+ | ^ 40 | - | .0248s | ||
+ | ^ 50 | - | .024s | | ||
+ | ^ ... | ... | .... | | ||
+ | ^ 100 | - | .0192s | ||
+ | ^ 200 | - | .024s | | ||
+ | ^ 300((300 was the chosen cutoff because at 400 we encounter a " | ||