Browse Source

LRU cache

pull/10/head
Jure Šorn 5 years ago
parent
commit
c47eb5ad3f
1 changed files with 16 additions and 0 deletions
  1. 16
      README.md

16
README.md

@ -621,6 +621,22 @@ def add(x, y):
return x + y
```
### LRU Cache
**Decorator that caches functions return values.**
```python
@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
```
```python
>>> [fib(n) for n in range(16)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
>>> fib.cache_info()
CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
```
Class
-----

Loading…
Cancel
Save