What about an EXPLICIT naming scheme for built-ins?

Alex Martelli aleaxit at yahoo.com
Thu Sep 9 18:41:41 CEST 2004


Raymond Hettinger <python at rcn.com> wrote:
   ...
> For clarity and conciseness, Alex's version wins easily.  For running
> speed, dis.dis() shows how the second has a simpler, more efficient
> inner loop:
> 
> [Alex's compact version]
>         >>   25 LOAD_FAST                1 (a)
>              28 JUMP_IF_FALSE           14 (to 45)
>              31 POP_TOP             
>              32 LOAD_GLOBAL              4 (heappop)
>              35 LOAD_FAST                1 (a)
>              38 CALL_FUNCTION            1
>              41 YIELD_VALUE         
>              42 JUMP_ABSOLUTE           25
> 
> [try/except version with localized call]
>   7     >>   34 LOAD_FAST                2 (pop)
>              37 LOAD_FAST                1 (a)
>              40 CALL_FUNCTION            1
>              43 YIELD_VALUE         
>              44 JUMP_ABSOLUTE           34

Heh -- neat.  According to timeit.py, on a random.shuffle'd range(1234),
on my machine (iBook G4 800), my version takes about 6.2 milliseconds
per loop; RH's takes 5.8; and my version with the 'pop=heappop'
optimization trick, about 6.  So, half of RH's version gains are in the
usual optimization trick (so delightfully eased by his own decorator
posted on the Cookbook, BTW), half are due to the slimmer, neater inner
loop bytecode.  While in this case a 3% speedup is probably no big deal,
I guess there may be cases in which it matters -- and Raymond shows us
all how to squeeze those few last %'s of optimization.

Of course, I _am_ now fully convinced that the alleged Raymond Hettinger
is actually the raybot, an enhanced version of the timbot -- after all,
have YOU ever seen Tim and RH in the same room at the same time?!

Anyway, my hat's off to a true master of optimization!


Alex



More information about the Python-list mailing list