Added import statement

This commit is contained in:
Brandon Rozek 2020-03-31 09:26:32 -04:00
parent a650fc940e
commit 38edd2a01c

View file

@ -8,6 +8,8 @@ tags: ["python"]
There is often a trade-off when it comes to efficiency of CPU vs memory usage. In this post, I will show how the [`lru_cache`](https://docs.python.org/3/library/functools.html#functools.lru_cache) decorator can cache results of a function call for quicker future lookup.
```python
from functools import lru_cache
@lru_cache(maxsize=2**7)
def fib(n):
if n == 1:
@ -19,4 +21,4 @@ def fib(n):
In the code above, `maxsize` indicates the number of calls to store. Setting it to `None` will make it so that there is no upper bound. The documentation recommends setting it equal to a power of two.
Do note though that `lru_cache` does not make the execution of the lines in the function faster. It only stores the results of the function in a dictionary.
Do note though that `lru_cache` does not make the execution of the lines in the function faster. It only stores the results of the function in a dictionary.