Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List concatenation using "+=" vs "extend" in Python 2 & Python 3

I was doing some analysis for doing list concatenation using two different approaches for varying lengths of lists, with different Python versions.

Using += in Python 2:

$ python -m timeit -s "li1=range(10**4); li2=range(10**4)" "li1 += li2"  
10000 loops, best of 3: 55.3 usec per loop  
$ python -m timeit -s "li1=range(10**6); li2=range(10**6)" "li1 += li2"  
100 loops, best of 3: 9.81 msec per loop  

Using += in Python 3:

$ python3 -m timeit -s "li1=list(range(10**4)); li2=list(range(10**4))" "`li1 += li2`"  
10000 loops, best of 3: 60 usec per loop  
$ python3 -m timeit -s "li1=list(range(10**6)); li2=list(range(10**6))" "`li1 += li2`"  
100 loops, best of 3: 11 msec per loop  

Using extend in Python 2:

$ python -m timeit -s "li1=range(10**4); li2=range(10**4)" "li1.extend(li2)"  
10000 loops, best of 3: 59 usec per loop  
$ python -m timeit -s "li1=range(10**6); li2=range(10**6)" "li1.extend(li2)"  
100 loops, best of 3: 10.3 msec per loop  

Using extend in Python 3:

$ python3 -m timeit -s "li1=list(range(10**4)); li2=list(range(10**4))" "li1.extend(li2)"  
10000 loops, best of 3: 64.1 usec per loop  
$ python3 -m timeit -s "li1=list(range(10**6)); li2=list(range(10**6))" "li1.extend(li2)"  
100 loops, best of 3: 11.3 msec per loop  
  1. I'm surprised to notice that Python 3 is much slower than Python 2, and the jump is huge.
  2. Another quite interesting thing is doing += is much more faster than doing an extend.

Any implementation reasons to justify the above observations, especially between Python 2 & Python 3?

like image 690
kmario23 Avatar asked Sep 02 '25 17:09

kmario23


1 Answers

I'm surprised to notice that Python 3 is much slower than Python 2, and the jump is huge.

As others have pointed out, your benchmarks don't support your statement that Python 3 is much slower. 9.81 msec vs 11 msec, 10.3 msec vs 11.3 msec?

It is a documented fact that Python 3 is a bit slower than Python 2. The What's new document states on performance:

The net result of the 3.0 generalizations is that Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5. Most likely the biggest cause is the removal of special-casing for small integers. There’s room for improvement, but it will happen after 3.0 is released!


Another quite interesting thing is doing += is much more faster than doing an extend.

Again, "much [more] faster" is exaggerated. 9.81 msec vs 10.3 msec?

For the answer, see this related SO question. extend requires a Python-level function call, vs += can be optimized at the C-level, so it is marginally faster.

like image 140
Ferdinand Beyer Avatar answered Sep 04 '25 08:09

Ferdinand Beyer