Profiling, sum-comprehension vs reduce

Raymond Hettinger python at
Tue Sep 16 18:19:07 CEST 2008

On Sep 13, 9:27 pm, Hrvoje Niksic <hnik... at> wrote:
> Note that, despite appearances, it's not as built-in as one might
> wish.  sum(seq) is still completely generic and works on all
> number-like objects (in fact on all objects that define an __add__
> operation except strings, which are explicitly forbidden).  This means
> that it can't just go ahead and execute a C addition, it must properly
> call PyNumber_Add (the C API call equivalent to Python's "+"
> operator), which will then inspect the objects and invoke the
> appropriate implementation of addition.

The time machine strikes again!  Try using Py2.6.
The built-in sum() function is much smarter and faster.
It does in fact use C addition.


More information about the Python-list mailing list