I sometimes use this trick, which I learnt from a book by Martelli.
Instead of try/except, membership testing with "in" (__contains__) might
be faster. Probably "depends". Matter of measuring.
def somefunc(arg, _cache={}):
if len(_cache) > 10 ** 5:
_cache.pop()
try:
return _cache[arg]
except KeyError:
result = expensivefunc(arg)
_cache[arg] = result
return result
Albert-Jan
--
https://mail.python.org/mailman/listinfo/python-list
- LRU cache Dino
- Re: LRU cache Chris Angelico
- RE: LRU cache avi.e.gross
- RE: LRU cache avi.e.gross
- Re: LRU cache Mats Wichmann
- Re: LRU cache Dino
- Re: LRU cache Weatherby,Gerard
- Re: LRU cache Dino
- Re: LRU cache Albert-Jan Roskam
- Re: LRU cache Thomas Passin
- Re: LRU cache Rob Cliffe via Python-list
- Re: LRU cache Albert-Jan Roskam
- Re: LRU cache Rob Cliffe via Python-list
