Have you ever written a Python function that seemed to take forever to execute? If you’re a fan of elegant, efficient code, you’re in for a treat. Today, I’m going to show you how to make your Python functions over 1000 times faster with just one line of code using automatic memoization. Let’s dive in!
The Problem: Slow Recursive Functions
Imagine you have a simple recursive function to calculate Fibonacci numbers:
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
for i in range(40):
print(fib(i))
At first glance, this code is clean and straightforward. However, it’s painfully slow for larger values of n
. Running this snippet can take around 52 seconds! Why? Because the fib()
function recalculates the same values multiple times, leading to an exponential increase in computation time.
The Magic Solution: Automatic Memoization with functools.cache
Enter the hero of our story: memoization. Memoization is a technique that stores the results of expensive function calls and returns the cached result when the same inputs occur again. In Python, the functools
library provides decorators to make memoization effortless.
Step 1: Using cache
for Automatic Memoization
Here’s how you can transform the slow Fibonacci function into a blazing-fast version with just one line of code:
from functools import cache
@cache
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
for i in range(40):
print(fib(i))
Boom! The execution time drops from 52 seconds to an astonishing 0.0136 seconds! How? The @cache
decorator remembers the results of previous function calls, eliminating redundant computations and skyrocketing your code’s performance.
Understanding the cache
Decorator
The cache
decorator works by storing the return values of the function in a cache (a special kind of dictionary). When you call the function with the same arguments, Python fetches the result directly from the cache instead of recalculating it. This simple addition not only speeds up your code but also keeps it clean and readable.
Optimizing Memory with lru_cache
While @cache
is fantastic for speed, it can consume a lot of memory if you’re caching a vast number of function calls. This is where the Least Recently Used (LRU) cache comes into play.
Step 2: Implementing lru_cache
for Better Memory Management
By using lru_cache
, you can limit the number of cached results, ensuring your program doesn’t use excessive memory:
from functools import lru_cache
@lru_cache(maxsize=5)
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
for i in range(40):
print(fib(i))
What’s Happening Here?
@lru_cache(maxsize=5)
tells Python to cache only the last 5 function calls.- This balances speed and memory usage, making your program more efficient without hogging resources.
Why This Matters
Automatic memoization transforms the way you write and optimize Python code. Here are a few reasons why you should embrace this technique:
- Speed Boost: Achieve up to 1000x faster execution with minimal code changes.
- Simplicity: With decorators like
@cache
and@lru_cache
, memoization is as easy as adding a single line. - Memory Efficiency: Control cache size with
lru_cache
to prevent excessive memory usage. - Readability: Keep your code clean and maintainable without intricate caching logic.
Final Thoughts
Memoization is a powerful tool in a Python developer’s arsenal. Whether you’re working on recursive functions, dynamic programming, or any computation-heavy tasks, decorators like @cache
and @lru_cache
can make your code not only faster but also more elegant.
So next time you find your Python code lagging, remember the magic of memoization. A single line of code could be the key to unlocking incredible performance improvements. Happy coding!
Quick Recap
- Problem: Slow recursive functions due to redundant calculations.
- Solution: Use
@cache
fromfunctools
to automatically memoize function results. - Optimization: Implement
@lru_cache(maxsize=5)
to balance speed and memory usage.
Embrace memoization and watch your Python code transform from sluggish to lightning-fast!
A Note From the Author
Thank you so much for taking the time to read the story. If you found my article helpful and interesting, please share your thoughts in the comment section, and don’t forget to share and clap
If you have enjoyed this post or any of my other work, I greatly appreciate donations from those who might be so inclined to support my writing. If you’d like to leave me a “tip,” I have an account set up HERE. Thank you!

Happy Coding!
If you’re a fan of Demon Slayer and want to bring a piece of the action home, check out these amazing toys on Amazon! Whether you’re collecting or gifting, these figures capture the essence of your favorite characters perfectly. Grab yours here!


Let’s Get in Touch!
- GitHub: @gajanan0707
- LinkedIn: Gajanan Rajput
- Website: mrcoder701.com
- YouTube: mrcoder701
- Instagram: mr_coder_701
- Medium: Gajanan Rajput
Greetings! Very helpful advice on this article! It is the little changes that make the biggest changes. Thanks a lot for sharing!