Here is the code I ran:
import timeitprint 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 * 1000
to the definition of x
gives the following results on my machine:
2.74775004387
2.7489669323
My best guess for the reason that sorted()
was slightly slower for you is that sorted()
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 for list.sort()
and sorted()
, so that's not what is causing the difference.
Edit: The source code of the current development version of sorted()
does the moral equivalent of
a = list(x)
a.sort()
and indeed, using this code instead of your second version eliminates any significant speed differences for any list sizes.