Speed: bytecode vz C API calls

Peter Otten __peter__ at web.de
Tue Dec 9 18:38:29 CET 2003

Jacek Generowicz wrote:

> aahz at pythoncraft.com (Aahz) writes:
>> Waitasec ....  okay, I see what your problem is, assuming you're
>> describing it correctly.  The problem is that you're calling Python
>> functions in an inner loop.
> ... indeedy ...
>> I think you're going to need to write a solution that doesn't call
>> functions (function unrolling, so to speak).  Instead of returning a
>> memoizer function, just use a dict of dicts.
> Sounds interesting. But how do I get the dictionary to use the
> function which it is memoizing, to calculate values which haven't been
> cached yet?

Guessing into the blue:

    result = cache[fun, args]
except KeyError:
    result = cache[fun, args] = fun(args)

should make a fast inner loop if cache misses are rare.

> Hmm ... maybe by subclassing dict, I can add the necessary
> functionality ... but even then I don't see how to avoid wrapping the
> dictionary access in a function call ... and then we're back to square
> one.

Subclassing dict will not make it faster. Maybe it's time to show some inner
loop lookalike to the wider public for more to the point suggestions...


More information about the Python-list mailing list