Your error in measurement is as follows: after your first call of test_list1.sort()
, that list object IS sorted — and Python’s sort, aka timsort, is wickedly fast on already sorted lists!!! That’s the most frequent error in using timeit
— inadvertently getting side effects and not accounting for them.
Here’s a good set of measurements, using timeit
from the command line as it’s best used:
$ python -mtimeit -s'import random; x=range(1000); random.shuffle(x)' '
y=list(x); y.sort()'
1000 loops, best of 3: 452 usec per loop
$ python -mtimeit -s'import random; x=range(1000); random.shuffle(x)' '
x.sort()'
10000 loops, best of 3: 37.4 usec per loop
$ python -mtimeit -s'import random; x=range(1000); random.shuffle(x)' '
sorted(x)'
1000 loops, best of 3: 462 usec per loop
As you see, y.sort()
and sorted(x)
are neck and neck, but x.sort()
thanks to the side effects gains over an order of magnitude’s advantage — just because of your measurement error, though: this tells you nothing about sort
vs sorted
per se! -)