Optimizing multiple dispatch

Jacek Generowicz jacek.generowicz at cern.ch
Thu Jun 3 16:55:19 CEST 2004

Jeff Epler <jepler at unpythonic.net> writes:

> Avoding map() may help:
>     tuple([type(a) for a in args])

Nonononononooooo!  :-)

> You could write a small extension to perform this operation without all
> the Python function calls.

[... Pyrex ...]
> 100000 loops, best of 3: 2.41 usec per loop
[... List comprehension ...]
> 10000 loops, best of 3: 25.3 usec per loop
[... map ...]
> 100000 loops, best of 3: 17 usec per loop

(I'm pretty sure that should be 10**5 loops in the list comp case)

Hmm, ok, the extension seems to be significantly faster. This
surprises me, because everything that executes in
"tuple(map(type,args))" is coded in C already, so I don't see where
the gain is coming from.

I've never got around to trying Pyrex ... and I won't be allowed to
depend on it formally. Would it be feasible to use Pyrex to generate
the extension source code and paste[*] that into my extension module?
IOW, is there a run-time dependence on Pyrex? 


> ... hmm, map is faster than listcomp.  my mistake!


I've asked for optimization advice a few times here, and usually the
responses include "replace map with a list comprehension" ... and yet
the map is always faster. I wonder where people get this idea that map
is slow ... until this started happening I had always assumed that
"everyone" knows that the map/reduce/filter family are the fastest
Python looping constructs.


[*] Ugh, I shudder at the thought, putting the products of code
    generators into CVS really goes against my fundamental principles.

More information about the Python-list mailing list