Re: [Python-Dev] sum(...) limitation

2014年8月12日 01:04:36 -0700

Hi all,
The core of the matter is that if we repeatedly __add__ strings from a
long list, we get O(n**2) behavior. For one point of view, the
reason is that the additions proceed in left-to-right order. Indeed,
sum() could proceed in a more balanced tree-like order: from [x0, x1,
x2, x3, ...], reduce the list to [x0+x1, x2+x3, ...]; then repeat
until there is only one item in the final list. This order ensures
that sum(list_of_strings) is at worst O(n log n). It might be in
practice close enough from linear to not matter. It also improves a
lot the precision of sum(list_of_floats) (though not reaching the same
precision levels of math.fsum()).
Just a thought,
Armin.
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to