[Python-ideas] Intermediate Summary: Fast sum() for non-numbers

Ron Adam ron3200 at gmail.com
Mon Jul 15 07:46:24 CEST 2013


On 07/14/2013 02:26 PM, Sergey wrote:
> * Sum is not obvious (for everyone) way to add lists, so people
>    should not use it, as there're alternatives, i.e. instead of
>    - sum(list_of_lists, [])
>    one can use:
>    - reduce(operator.iadd, list_of_lists, [])
>    - list(itertools.chain.from_iterable(list_of_lists))
>    - result = []
>      for x in list_of_lists:
>          result.extend(x)

I have nothing against increasing sum()'s speed.


I think the real issue is having a very fast way to sum non-numbers.


You could copy the code from sum() and make a function with a different 
name that specialises in non-number addition.  That would not have any 
backwards compatibility issues.


Do you think you could use what you learned with sum to make chain, or a 
new fold function faster?

Both of those have some advantages over sum(), but aren't as fast.  If you 
could make those faster, then that would be very nice.


The advantages are..

Chain works with most iterables and uses much less memory in some cases.

A new fold function would do quite a lot more depending on the operator 
passed to it.  It may be possible to speed up some common cases that use 
methods on builtin types.

Cheers,
    Ron







More information about the Python-ideas mailing list