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

Steven D'Aprano steve at pearwood.info
Thu Jul 11 03:03:40 CEST 2013


On 11/07/13 07:00, Ron Adam wrote:
>
>
> On 07/10/2013 12:49 PM, Steven D'Aprano wrote:
>> On 11/07/13 02:10, Sergey wrote:
>>> On Jul 9, 2013 Steven D'Aprano wrote:
>>>
>>>> The fact that sum(lists) has had quadratic performance since sum
>>>> was first introduced in Python 2.3, and I've *never* seen anyone
>>>> complain about it being slow, suggests very strongly that this is not
>>>> a use-case that matters.
>>>
>>> Never seen? Are you sure? ;)
>>>> http://article.gmane.org/gmane.comp.python.general/658630
>>>> From: Steven D'Aprano @ 2010-03-29
>>>> In practical terms, does anyone actually ever use sum on more than a
>>>> handful of lists? I don't believe this is more than a hypothetical
>>>> problem.
>>
>> Yes, and I stand by what I wrote back then.
>
>
> Just curious, how does your sum compare with fsum() in the math module?

math.fsum is a high-precision floating point sum, keeping extra precision that the built-in loses. Compare these:

data = [1e-100, 1e100, 1e-100, -1e100]*1000
sum(data)
math.fsum(data)

The exact value for the sum is 2e-97.



-- 
Steven


More information about the Python-ideas mailing list