Skip to content

Instantly share code, notes, and snippets.

@jonathanslenders
Created June 3, 2022 10:18
Show Gist options
  • Select an option

  • Save jonathanslenders/6b39adbcabab3b93b662dc04810a1001 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanslenders/6b39adbcabab3b93b662dc04810a1001 to your computer and use it in GitHub Desktop.
Comparing the performance of a cache built using a `dict` + `__missing__` against functools.cache
* Super-optimized for small spaces - read how we shrank the memory │ Date: Tue May 31 22:09:56 2022 +0200
"""
Comparing the performance of a cache built using a `dict` and `__missing__` with functools.cache
"""
import time
from typing import TypeVar, Callable, Dict
from functools import cache
_K = TypeVar("_K")
_V = TypeVar("_V")
class FastDictCache(Dict[_K, _V]):
"""
LRU cache implementation using dict.
"""
def __init__(self, get_value: Callable[..., _V]) -> None:
self.get_value = get_value
def __missing__(self, key: _K) -> _V:
result = self.get_value(key)
self[key] = result
return result
# Function that we want to cache.
def func(param: tuple[int, int]) -> int:
a, b = param
return a + b
# Benchmark dict cache.
dict_cache = FastDictCache(func)
start = time.time()
for i in range(100000):
for i in range(1000):
result = dict_cache[1, 2]
end = time.time()
print("FastDictCache duration", end - start)
# Benchmark functools.cache.
cached_func = cache(func)
start = time.time()
for i in range(100000):
for i in range(1000):
result = cached_func((1, 2))
end = time.time()
print("functools.cache duration", end - start)
# Without cache.
start = time.time()
for i in range(100000):
for i in range(1000):
result = func((1, 2))
end = time.time()
print("without cache duration", end - start)
# Results, Python 3.9.10, Linux
"""
(in seconds:)
FastDictCache duration 10.623250484466553
functools.cache duration 14.37010645866394
without cache duration 14.477365255355835
"""
# Results, Python 3.11.0b3
"""
FastDictCache duration 11.265424013137817
functools.cache duration 17.75334858894348
without cache duration 12.926888227462769
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment