Speed: bytecode vz C API calls
Stuart Bishop
stuart.b at commonground.com.au
Tue Dec 9 21:00:51 EST 2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 10/12/2003, at 8:55 AM, Jacek Generowicz wrote:
> Peter Otten <__peter__ at web.de> writes:
>
>> Jacek Generowicz wrote:
>>
>>> aahz at pythoncraft.com (Aahz) writes:
>>>
>>>> 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:
>>
>> try:
>> result = cache[fun, args]
>> except KeyError:
>> result = cache[fun, args] = fun(args)
>
> Allow me to remind you of the memoizer I posted at the top of the
> thread:
>
> def memoize(callable):
> cache = {}
> def proxy(*args):
> try: return cache[args]
> except KeyError: return cache.setdefault(args,
> callable(*args))
> return proxy
>
> One key feature of this is that I can use it to replace any[*]
> function with a functionally equivalent but faster one, without having
> to change any of the call sites of the original.
> proxy does. However, it's the "almost" that creates the problem. If
> I'm prepared to mess around with the calls to the original functions
> and replace them with your suggestion, then, of course the problem is
> solved ... however, I _really_ do not want to replace the calls to the
> original ...
About the only thing that could be done then is:
def memoize(callable):
cache = {}
def proxy(*args, _cache=cache):
try: return _cache[args]
except KeyError: return cache.setdefault(args, callable(*args))
return proxy
Might help a little bit, although probably not enough.
- --
Stuart Bishop <stuart at stuartbishop.net>
http://www.stuartbishop.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (Darwin)
iD8DBQE/1n5TAfqZj7rGN0oRAlSQAJ9WZe39F8L1KECCMTS3HxUnWKGx8wCgjZ8P
68pLTP33rlFI0eSEHK8FVXY=
=25Fx
-----END PGP SIGNATURE-----
More information about the Python-list
mailing list