Skip to content

Instantly share code, notes, and snippets.

@mekarpeles
Created August 20, 2012 21:30
Show Gist options
  • Save mekarpeles/3408081 to your computer and use it in GitHub Desktop.
Save mekarpeles/3408081 to your computer and use it in GitHub Desktop.
Python 2.7 Performance Comparison: list concatenation (+) vs list.extend()
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
@nik312123
Copy link

@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