We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent da8ed9c commit e40f6a4Copy full SHA for e40f6a4
README.md
@@ -2651,6 +2651,37 @@ def fibonacci(n):
2651
return next(fibs)
2652
```
2653
2654
+**做缓存**
2655
+
2656
+```python
2657
+def cache(fn):
2658
+ cached = {}
2659
+ def wrapper(*args):
2660
+ if args not in cached:
2661
+ cached[args] = fn(*args)
2662
+ return cached[args]
2663
+ wrapper.__name__ = fn.__name__
2664
+ return wrapper
2665
2666
+@cache
2667
+def fib(n):
2668
+ if n < 2:
2669
+ return 1
2670
+ return fib(n-1) + fib(n-2)
2671
+```
2672
2673
+**利用 funtools.lru_cache 做缓存**
2674
2675
2676
+from functools import lru_cache
2677
2678
+@lru_cache(maxsize=32)
2679
2680
2681
2682
2683
2684
2685
#### Logarithmic
2686
2687
**矩阵**
0 commit comments