Created
June 13, 2014 08:36
-
-
Save lsbardel/c624e667501bfd773194 to your computer and use it in GitHub Desktop.
python list insert versus addition
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
import timeit | |
def make(n): | |
return list(range(n)) | |
def add(v): | |
return ['bla'] + v | |
def insert(v): | |
v.insert(0, 'bla') | |
return v | |
if __name__=='__main__': | |
repeat = 10000 | |
for n in (10, 100, 1000): | |
pre = "from __main__ import add, insert, make\nv = make(%d)" % n | |
t = timeit.Timer("add(v)", pre) | |
print('add to %d long list: %s' % (n, t.timeit(repeat))) | |
t = timeit.Timer("insert(v)", pre) | |
print('insert to %d long list: %s' % (n, t.timeit(repeat))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment