DEV Community

niuniu
niuniu

Posted on

Quick Tip — Python's functools.cache Cut My Script's Runtime by 94%

Quick Tip

I had a data-cleaning script taking 8 minutes 12 seconds. One decorator got it to 29 seconds. No async, no multiprocessing — just this:

from functools import cache

@cache
def normalize_sku(raw: str) -> str:
    # expensive regex chain + lookup, called 400k times
    # but only ~1,900 unique inputs
    ...
Enter fullscreen mode Exit fullscreen mode

functools.cache (Python 3.9+) memoizes the function: same input → instant return of the stored result. It's an unbounded version of lru_cache(maxsize=None):

from functools import lru_cache

@lru_cache(maxsize=10_000)  # bounded — evicts least-recently-used
def geocode(city: str) -> tuple[float, float]:
    return api_lookup(city)
Enter fullscreen mode Exit fullscreen mode

The data

Variant Runtime Notes
No cache 8m 12s 400k calls, 1.9k unique
@cache 0m 29s 94% faster
@lru_cache(2000) 0m 31s nearly identical here

Rules of thumb:

  • Only for pure functions (same input → same output, no side effects)
  • Arguments must be hashable (no lists/dicts as params)
  • Use @lru_cache(maxsize=N) if inputs are unbounded — @cache never evicts and can eat RAM
  • It's per-process; restarted scripts start cold

I discover half my stdlib tricks by asking MonkeyCode (free, open-source) "is there a builtin for X" before reaching for a dependency: https://ly.cyberserval.tech/iIETXiF

What's your favorite one-line Python speedup?

Top comments (0)