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@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@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@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@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 <rep...@bugs.python.org>
<http://bugs.python.org/issue18424>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to