Why is Python's "sorted()" slower than "copy, then .sort()"

Viewed 4226

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

2 Answers
Related