Here is the code I ran:
import timeit
print timeit.Timer('''a = sorted(x)''', '''x = [(2, 'bla'), (4, 'boo'), (3, 4), (1, 2) , (0, 1), (4, 3), (2, 1) , (0, 0)]''').timeit(number = 1000)
print timeit.Timer('''a=x[:];a.sort()''', '''x = [(2, 'bla'), (4, 'boo'), (3, 4), (1, 2) , (0, 1), (4, 3), (2, 1) , (0, 0)]''').timeit(number = 1000)
and here are the results:
0.00259663215837
0.00207390190177
I would like to know why using .sort() is consistently faster than sorted() even though both are copying lists?
Note: I am running Python 2.7 on an 2.53Ghz i5 with Win7
The difference you are looking at is miniscule, and completely goes away for longer lists. Simply adding
* 1000to the definition ofxgives the following results on my machine:My best guess for the reason that
sorted()was slightly slower for you is thatsorted()needs to use some generic code that can copy any iterable to a list, while copying the list directly can make the assumption that the source is also a list. The sorting code used by CPython is actually the same forlist.sort()andsorted(), so that’s not what is causing the difference.Edit: The source code of the current development version of
sorted()does the moral equivalent ofand indeed, using this code instead of your second version eliminates any significant speed differences for any list sizes.