Re: @lru_cache on functions with no arguments

2017-08-03 Thread Ian Kelly
On Thu, Aug 3, 2017 at 12:17 PM, Ian Kelly wrote: > On Thu, Aug 3, 2017 at 11:18 AM, Serhiy Storchaka wrote: >> $ ./python -m timeit -s 'import sys; sys.modules["_functools"] = None; del >> sys.modules["functools"]; from functools import lru_cache; f = >> lru_cache(maxsize=None)(int)' -- 'f()' >>

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Ian Kelly
On Thu, Aug 3, 2017 at 11:18 AM, Serhiy Storchaka wrote: > $ ./python -m timeit -s 'import sys; sys.modules["_functools"] = None; del > sys.modules["functools"]; from functools import lru_cache; f = > lru_cache(maxsize=None)(int)' -- 'f()' > 10 loops, best of 5: 3.39 usec per loop Interesting

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Serhiy Storchaka
03.08.17 19:08, Ian Kelly пише: 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: It didn't work because the functools module already was imported at startup. $ ./python -m

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Ian Kelly
On Thu, Aug 3, 2017 at 10:02 AM, Ian Kelly wrote: > Fixed: > > $ python3 -m timeit -s 'from simple_cache import simple_cache; f = > simple_cache(int)' -- 'f()' > 100 loops, best of 3: 0.167 usec per loop > $ python3 -m timeit -s 'import sys; sys.modules["_functools"] = None; > from functools i

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Ian Kelly
On Thu, Aug 3, 2017 at 9:55 AM, Serhiy Storchaka wrote: > 03.08.17 18:36, Ian Kelly пише: >> >> The single variable is only a dict lookup if it's a global. Locals and >> closures are faster. >> >> def simple_cache(function): >> sentinel = object() >> cached = sentinel >> >> @functoo

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Ian Kelly
On Thu, Aug 3, 2017 at 9:44 AM, wrote: > I hope you washed them! Yes, well as noted in my followup I was comparing pure-Python implementations, not the C implementation. -- https://mail.python.org/mailman/listinfo/python-list

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Serhiy Storchaka
03.08.17 18:36, Ian Kelly пише: The single variable is only a dict lookup if it's a global. Locals and closures are faster. def simple_cache(function): sentinel = object() cached = sentinel @functools.wraps(function) def wrapper(*args, **kwargs): nonlocal cached

Re: @lru_cache on functions with no arguments

2017-08-03 Thread tom
On Thursday, 3 August 2017 16:37:22 UTC+1, Ian wrote: > On Thu, Aug 3, 2017 at 8:35 AM, Paul Moore ...@gmail.com> wrote: > > On Tuesday, 1 August 2017 15:54:42 UTC+1, t...@tomforb.es wrote: > >> > _sentinel = object() > >> > _val = _sentinel > >> > def val(): > >> > if _val is _sentinel: > >

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Ian Kelly
On Thu, Aug 3, 2017 at 9:36 AM, Ian Kelly wrote: > On Thu, Aug 3, 2017 at 8:35 AM, Paul Moore wrote: >> On Tuesday, 1 August 2017 15:54:42 UTC+1, t...@tomforb.es wrote: >>> > _sentinel = object() >>> > _val = _sentinel >>> > def val(): >>> > if _val is _sentinel: >>> > # Calculate _

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Ian Kelly
On Thu, Aug 3, 2017 at 8:35 AM, Paul Moore wrote: > On Tuesday, 1 August 2017 15:54:42 UTC+1, t...@tomforb.es wrote: >> > _sentinel = object() >> > _val = _sentinel >> > def val(): >> > if _val is _sentinel: >> > # Calculate _val >> > return _val >> > >> > seems entirely sufficie

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Paul Moore
On Tuesday, 1 August 2017 15:54:42 UTC+1, t...@tomforb.es wrote: > > _sentinel = object() > > _val = _sentinel > > def val(): > > if _val is _sentinel: > > # Calculate _val > > return _val > > > > seems entirely sufficient for this case. Write a custom decorator if you > > use th

Re: @lru_cache on functions with no arguments

2017-08-02 Thread Steven D'Aprano
On Tue, 01 Aug 2017 11:05:38 -0400, Terry Reedy wrote: > On 8/1/2017 7:06 AM, Matt Wheeler wrote: >> On Tue, 1 Aug 2017 at 02:32 Terry Reedy wrote: >> >>> On 7/31/2017 7:31 PM, t...@tomforb.es wrote: As part of the Python 3 cleanup in Django there are a fair few uses of >>> @functools.

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Terry Reedy
On 8/1/2017 7:06 AM, Matt Wheeler wrote: On Tue, 1 Aug 2017 at 02:32 Terry Reedy wrote: On 7/31/2017 7:31 PM, t...@tomforb.es wrote: As part of the Python 3 cleanup in Django there are a fair few uses of @functools.lru_cache on functions that take no arguments. This makes no sense to me. I

Re: @lru_cache on functions with no arguments

2017-08-01 Thread tom
> _sentinel = object() > _val = _sentinel > def val(): > if _val is _sentinel: > # Calculate _val > return _val > > seems entirely sufficient for this case. Write a custom decorator if you use > the idiom often enough to make it worth the effort. I did some timings with this as p

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Christian Heimes
On 2017-08-01 01:31, t...@tomforb.es wrote: > As part of the Python 3 cleanup in Django there are a fair few uses of > @functools.lru_cache on functions that take no arguments. A lru_cache isn't > strictly needed here, but it's convenient to just cache the result. Some > examples are here: https

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Paul Moore
On Tuesday, 1 August 2017 00:31:52 UTC+1, t...@tomforb.es wrote: > Am I right in thinking that using `maxsize=None` is best for functions that > accept no arguments? Should we even be using a `lru_cache` in such > situations, or write our own simple cache decorator instead? It seems like overki

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Matt Wheeler
On Tue, 1 Aug 2017 at 12:53 Thomas Nyberg wrote: > On 08/01/2017 01:06 PM, Matt Wheeler wrote: > > A function which is moderately expensive to run, that will always return > > the same result if run again in the same process, and which will not be > > needed in every session. > > > > What about j

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Thomas Nyberg
On 08/01/2017 02:50 PM, t...@tomforb.es wrote: > 2. Django has a long-standing no-dependencies rule, which may change in the > near future but for now it is stdlib only. We can't add a dependency on > `lazy-property`. Apologies for continuing going off-topic, but the actual code in that package I

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Chris Angelico
On Tue, Aug 1, 2017 at 10:50 PM, wrote: > And you have a simple function: > > def test(): >return object() > > I get the following numbers without much variance: > > 1. lru_cache(maxsize=None) - 870ns > > 2. lru_cache() - 1300ns > > 3. no cache - 100ns > > So, in the best case, without the C

Re: @lru_cache on functions with no arguments

2017-08-01 Thread tom
Hello all, Thank you for the replies! So, here is the context: 1. The functions Django wants to cache require Django to be initialized and the settings loaded. This means the return values are not available at definition time. (Matt Wheeler hits it on the head). 2. Django has a long-standing no

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Thomas Nyberg
On 08/01/2017 01:06 PM, Matt Wheeler wrote: > A function which is moderately expensive to run, that will always return > the same result if run again in the same process, and which will not be > needed in every session. > What about just using a lazy getter property? E.g.: https://pypi.p

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Matt Wheeler
On Tue, 1 Aug 2017 at 02:32 Terry Reedy wrote: > On 7/31/2017 7:31 PM, t...@tomforb.es wrote: > > As part of the Python 3 cleanup in Django there are a fair few uses of > @functools.lru_cache on functions that take no arguments. > > This makes no sense to me. If the function is being called for

Re: @lru_cache on functions with no arguments

2017-07-31 Thread Terry Reedy
On 7/31/2017 7:31 PM, t...@tomforb.es wrote: As part of the Python 3 cleanup in Django there are a fair few uses of @functools.lru_cache on functions that take no arguments. This makes no sense to me. If the function is being called for side-effects, then it should not be cached. If the fun

Re: @lru_cache on functions with no arguments

2017-07-31 Thread codewizard
On Monday, July 31, 2017 at 7:31:52 PM UTC-4, t...@tomforb.es wrote: > As part of the Python 3 cleanup in Django there are a fair few uses of > @functools.lru_cache on functions that take no arguments. A lru_cache isn't > strictly needed here, but it's convenient to just cache the result. Some >

@lru_cache on functions with no arguments

2017-07-31 Thread tom
As part of the Python 3 cleanup in Django there are a fair few uses of @functools.lru_cache on functions that take no arguments. A lru_cache isn't strictly needed here, but it's convenient to just cache the result. Some examples are here: https://github.com/django/django/pull/8825/files I did s