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
+=
is much more faster than doing an extend
.Any implementation reasons to justify the above observations, especially between Python 2 & Python 3?
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 anextend
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With