Back
Close

Journey to Master Python (WIP)

ageekymonk
13.4K views

Functools module

Memoization

Functools module supports memoization through lru_cache decorator. This way you dont need to write memoization code.

In the example adding the decorator @lru_cache does the needful. You can also see the cache details with cache_info

lru_cache Decorator
import time
from functools import lru_cache
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants