Describe the Use of `functools.lru_cache` for Memoization

·

`functools.lru_cache` caches the results of expensive function calls. It improves performance by avoiding redundant calculations.

Example:

from functools import lru_cache

@lru_cache(maxsize=3)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

print(fib(10))

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *