Speed: bytecode vz C API calls

Bengt Richter bokr at oz.net
Fri Dec 12 12:52:53 CET 2003


On 12 Dec 2003 11:17:16 +0100, Jacek Generowicz <jacek.generowicz at cern.ch> wrote:

>bokr at oz.net (Bengt Richter) writes:
>
>> On Tue, 9 Dec 2003 23:50:06 -0500, "Francis Avila" <francisgavila at yahoo.com> wrote:
>> 
>> >Stuart Bishop wrote in message ...
>> >>About the only thing that could be done then is:
>> >>
>> >>def memoize(callable):
>> >>    cache = {}
>> >>    def proxy(*args, _cache=cache):
>
>To be honest, I have trouble believing that a local variable lookup
>would be significantly faster than a closure lookup ... or is there
>some other point to this ?
No, I think you are right. I was mainly reacting to the "there's no way" statement ;-)

>
>> >Not legal.  *args must always come after default args. There's no way to do
>> Never say never ;-)
>> A function has the __get__ method, which is what getattr magic uses
>> to make a bound method.  You can use this put cache in the "self"
>> position of a bound method, like
>> 
>>  >>> def sq(x): return x*x
>>  ...
>>  >>> def memoize(func): # callable is a builtin ;-)
>>  ...     def proxy(cache, *args):
>>  ...         try: return cache[args]
>>  ...         except KeyError: return cache.setdefault(args, func(*args))
>>  ...     return proxy.__get__({}, proxy.__class__)
>
>
>> Looking at the code, it might be worth timing, if you have 90%+ cache hits.
>> I'd be curious to see what you get
>
>I get your version being almost imperceptibly slower.
>
>I used this:
When things are that close, it's very hard to draw conclusions except that they are
probably close ;-) But to split hairs you probably have to run the two tests separately,
in quiescent system situations, so that the first is not improving or worsening the
environment for the second (i.e., in terms of interpreter state re memory, or even cpu
cache in some cases, though that usually only applies to the first time through a loop.
And also keep the fastest of 5-10 trials, to eliminate glitches due to spurious irrelevant
system activity. I don't have your "timing" module, so I don't know what it does. If it
randomly reordered multiloop trials of A vs B and took minimums, then maybe interactions
could be washed out, but otherwise A & B in the same test source leaves some ambiguity
about the real ranking (which of course will not matter, practically speaking;-)

>
>import timing
>import sys
[...]

Regards,
Bengt Richter




More information about the Python-list mailing list