This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
python3:lru_cache [2019/05/07 12:02] jguerin Finished table. |
python3:lru_cache [2019/05/07 12:43] (current) jguerin |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | bb====== LRU Cache ====== | + | ====== LRU Cache ====== |
Memoization is a common optimization technique where repeatedly computed values are cached for quick lookup. While memoization can be achieved by a relatively simple modification to many recursive formulations, | Memoization is a common optimization technique where repeatedly computed values are cached for quick lookup. While memoization can be achieved by a relatively simple modification to many recursive formulations, | ||
===== 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 28: | Line 31: | ||
==== Benchmarks ==== | ==== Benchmarks ==== | ||
- | ((Python 3.5.2)) | + | The naive recursive implementation fails quickly as expected |
- | ^ %%n%% ^ fibonacci.py | + | ^ %%n%% ^ fibonacci.py((Python 3.5.2)) |
^ 10 | .016s | .0224s | ^ 10 | .016s | .0224s | ||
^ 20 | .0216s | ^ 20 | .0216s | ||
Line 39: | Line 42: | ||
^ 100 | - | .0192s | ^ 100 | - | .0192s | ||
^ 200 | - | .024s | | ^ 200 | - | .024s | | ||
- | ^ 300 | - | .0232s | + | ^ 300((300 was the chosen cutoff because at 400 we encounter a " |