@lru_cache on functions with no arguments

Ian Kelly ian.g.kelly at gmail.com
Thu Aug 3 12:08:42 EDT 2017


On Thu, Aug 3, 2017 at 10:02 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> Fixed:
>
> $ python3 -m timeit -s 'from simple_cache import simple_cache; f =
> simple_cache(int)' -- 'f()'
> 1000000 loops, best of 3: 0.167 usec per loop
> $ python3 -m timeit -s 'import sys; sys.modules["_functools"] = None;
> from functools import lru_cache; f = lru_cache(maxsize=None)(int)' --
> 'f()'
> 1000000 loops, best of 3: 0.783 usec per loop

This turns out to be because I was running 3.4 which doesn't have the
C implementation to begin with. In 3.6 this trick doesn't seem to work
as expected for disabling it:

$ python3.6 -m timeit -s 'from simple_cache import simple_cache; f =
simple_cache(int)' -- 'f()'
10000000 loops, best of 3: 0.152 usec per loop
$ python3.6 -m timeit -s 'from functools import lru_cache; f =
lru_cache(maxsize=None)(int)' -- 'f()'
10000000 loops, best of 3: 0.0422 usec per loop
$ python3.6 -m timeit -s 'import sys; sys.modules["_functools"] =
None; from functools import lru_cache; f =
lru_cache(maxsize=None)(int)' -- 'f()'
10000000 loops, best of 3: 0.0419 usec per loop



More information about the Python-list mailing list