Optimizing multiple dispatch
jacek.generowicz at cern.ch
Thu Jun 3 17:14:39 CEST 2004
Jacek Generowicz <jacek.generowicz at cern.ch> writes:
> 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.
Forgot to mention: your map and list comp versions included an
unnecessary call to a pure Python function (maptype). The extension
you proposed would replace an inlined "tuple(map(type, args))", so we
don't get hit by pure Python function call overhead at that point[*].
Still, eliminating that overhead knocks off only one third of the
time, while your extension was better by a factor of 7 ... so there's
still a factor of 4.5ish to be had by coding it as an extension
(sorry, I don't have Pyrex, so I can't show the time):
-s 's = ("", 0, 0.0, 0L, str, int, type)' -s 'def maptype(s): return tuple(map(type, s))' 'maptype(s)'
100000 loops, best of 3: 4.73 usec per loop
-s 's = ("", 0, 0.0, 0L, str, int, type)' 'tuple(map(type, s))'
100000 loops, best of 3: 4.46 usec per loop
[*] There is a pure Python function call overhead I would like to
eliminate by recoding in C, but it concerns a closure (which Pyrex
doesn't allow, IIRC), and that seems a bit tricky to do.
More information about the Python-list