Speed: bytecode vz C API calls

Aahz aahz at pythoncraft.com
Mon Dec 8 13:04:59 EST 2003


In article <tyf7k171jbq.fsf at pcepsft001.cern.ch>,
Jacek Generowicz  <jacek.generowicz at cern.ch> wrote:
>
>I have a program in which I make very good use of a memoizer:
>
>  def memoize(callable):
>      cache = {}
>      def proxy(*args):
>          try: return cache[args]
>          except KeyError: return cache.setdefault(args, callable(*args))
>      return proxy
>
>I've got to the stage where my program is still not fast enough, and
>calls to the memoizer proxy are topping profiler output table. So I
>thought I'd try to see whether I can speed it up by recoding it in C.

I'm wondering whether you're tackling something the wrong way or perhaps
you need to throw hardware at the problem.  Dicts are one of the most
highly optimized parts of Python, and if your cache hit rate is higher
than 90%, you'll be dealing with exceptions only rarely, so the speed of
cache.setdefault() isn't where your time is going.  I'm wondering whether
you're hitting some kind of memory issue or something, or possibly you're
having cache effects (in CPU/memory), or maybe even your key is poorly
constructed so that you're getting hash duplicates.

OTOH, if the cache hit rate is smaller than that, you'll be paying the
penalty for raised exceptions *and* the callable() call, in which case
redesigning that part will pay the highest dividends.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.




More information about the Python-list mailing list