Created
August 20, 2012 21:30
-
-
Save mekarpeles/3408081 to your computer and use it in GitHub Desktop.
Python 2.7 Performance Comparison: list concatenation (+) vs list.extend()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mek@bigbertha:~/7-Projects$ python | |
Python 2.7.2+ (default, Oct 4 2011, 20:06:09) | |
[GCC 4.6.1] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import time | |
>>> def test(): | |
... """Comapring concat. + vs list.extend""" | |
... x = range(10000000) | |
... y = range(10000000) | |
... x0 = time.clock() | |
... z = x + y | |
... t1 = time.clock() - x0 | |
... x1 = time.clock() | |
... x.extend(y) | |
... t2 = time.clock() - x1 | |
... print "+ takes %s, extend takes %s" % (t1, t2) | |
... | |
>>> test() | |
+ takes 0.31, extend takes 0.12 |
@colllin This is so long enough ago that I don't remember the context of this. But yeah. I'm fairly sure .extend(...)
is an in-place operation and +=
is not (creates a new list).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But extend is in-place and adding like this is not (compared to
+=
)... right?