[docs] [issue18424] sum() does not return only the sum of a sequence of numbers + PEP8 reccomandation

Ronald Oussoren report at bugs.python.org
Wed Jul 10 17:33:08 CEST 2013


Ronald Oussoren added the comment:

Appending a sequence of lists with sum is inefficient because it (currently) does a lot of copying, and that gets noticable when you sum a larger number of lists

Note how using sum for add 200 lists is more than twice as long as adding 100 lists:

ronald at gondolin[0]$ python -m timeit -s "lists=[['a']*100 for i in range(100)]" "sum(lists, [])"
100 loops, best of 3: 2.04 msec per loop


ronald at gondolin[0]$ python -m timeit -s "lists=[['a']*100 for i in range(200)]" "sum(lists, [])"
100 loops, best of 3: 9.2 msec per loop


Also note how using itertools.chain is both a lot faster and behaves better:

ronald at gondolin[0]$ python -m timeit -s "import itertools; lists=[['a']*100 for i in range(100)]" "list(itertools.chain.from_iterable(lists))"
10000 loops, best of 3: 165 usec per loop


ronald at gondolin[0]$ python -m timeit -s "import itertools; lists=[['a']*100 for i in range(100)]" "list(itertools.chain.from_iterable(lists))"
10000 loops, best of 3: 155 usec per loop

(I used python2.7 for this, the same behavior can be seem with python 3).

See also #18305, which proposed a small change to how sum works which would fix the performance problems for summing a sequence of lists (before going too far and proposing to add special-case tuples and string)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18424>
_______________________________________


More information about the docs mailing list