Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
How-tos

Speed Up Python Code with lru_cache

Learn how to use functools.lru_cache to instantly speed up repetitive function calls in Python with just one line of code. A simple caching trick for pure, expensive functions.

July 2026 4 min read 3 views 0 hearts

Here is the article you requested, written for PythonSkillset.com.


Speed Up Your Python Code with One Line

Ever written a function that just takes forever to run? Maybe it’s fetching data from a slow API, processing a large file, or performing a complex calculation. You call it once, wait. You call it again with the same input, and you wait all over again. That’s frustrating.

There’s a neat trick in Python’s standard library that can save you from this pain. It’s called lru_cache, and it lives in the functools module. The best part? You can add it with just one line of code.

What is lru_cache?

lru_cache is a decorator that remembers the results of your function calls. Think of it as a little notepad. When you call a function with a specific argument, it writes down the result. The next time you call it with the same argument, instead of running the whole function again, it just looks up the answer from its notepad and returns it instantly.

"LRU" stands for "Least Recently Used". This just means the cache has a size limit. If you keep adding new results and the notepad gets full, it throws away the oldest, least-used entry to make space for the new one. This prevents your program from eating up all your memory.

How to Use It

Let’s look at a simple example. Say you have a function that calculates the nth Fibonacci number. The recursive version is famously slow because it recalculates the same values over and over.

def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

Calling fib(35) will make you wait for a second or two. Calling it again? You wait again.

Now, let's add the magic line.

from functools import lru_cache

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

That @lru_cache(maxsize=None) is the decorator. maxsize=None means the cache can grow indefinitely. Now, try calling fib(35).

The first call might take a second. But the second call? It returns the result instantly. The function gets called only once for each unique input.

When Should You Use It?

You want to use lru_cache when your function is pure and expensive.

  • Pure function: The function's output depends only on its inputs, and it has no side effects (like writing to a file or changing a global variable). If the function returns different results for the same input, you should not cache it.
  • Expensive function: The function takes a noticeable amount of time to run, and it gets called with the same arguments repeatedly.

Real-world examples from PythonSkillset readers:

  • Database queries: You fetch user details by user ID. The lru_cache can hold the results for the most recently queried users, saving you a trip to the database.
  • Configuration parsing: Your app reads a config file at startup. Using lru_cache on the parsing function means you only read the file once, even if multiple modules import it.
  • API calls: If you need to fetch exchange rates or weather data and the data doesn't change every second, a short-lived cache can save you from rate limits.

A Word of Caution

The cache lives in memory. If your function takes a dictionary or a list as an argument, Python has to create a hash of that argument to store it. Mutable objects like lists don't have a fixed hash, so lru_cache won't work directly with them. You'd need to convert them to immutable types like tuples.

Also, be careful with maxsize=None. If you cache the result of every unique request a user makes, your server's memory will eventually fill up. In production code, you usually set a sensible maxsize, like 128 or 256.

The Big Picture

lru_cache is one of those tools that makes you smile when you learn about it. It’s a simple, built-in solution to a common performance problem. You don’t need to install a third-party library or write complex caching logic. Just import, decorate, and watch your slow functions become fast.

Next time you notice your script pausing on a repeated calculation, give lru_cache a try. It might just save your afternoon.

Comments

Questions, corrections, and tips stay visible for everyone reading this page.

0 in thread

Join the discussion

Shown next to your comment.

Up to 4,000 characters

No comments yet

Be the first to leave a note — it helps the next reader.