In reference to this older stack overflow question @Raymond Hettinger gets presumeably correct results where Counter
is 4x faster then sorted
using timeit from the command line like so:
python3.6 -m timeit -s 'from collections import Counter' -s 'from random import shuffle' -s 't=list(range(100)) * 5' -s 'shuffle(t)' -s 'u=t[:]' -s 'shuffle(u)' 'Counter(t)==Counter(u)'
My results indicate that sorted
is significantly faster then Counter
! Am I using timeit incorrectly? Interpreting the results wrong? Is the setup data somehow producing different results?
import timeit, functools
from collections import Counter
def sorted_lists(l1,l2):
return sorted(l1) == sorted(l2)
def counted_lists(l1,l2):
return Counter(l1) == Counter(l2)
short1 = [0,1,2,3,4,5,5]
short2 = [0,1,5,3,4,5,2]
long1 = list(range(0, 1000)) + [100, 10, 1000, 5]
long2 = list(range(0, 1000)) + [5, 10, 100, 1000]
number = 1000
# Long test
t = timeit.Timer(lambda: sorted_lists(long1, long2))
rl1 = t.timeit(number)
print("sorted long :{}".format(rl1))
t = timeit.Timer(lambda: counted_lists(long1, long2))
rl2 = t.timeit(number)
print("counted long :{}".format(rl2)
# Short test
t = timeit.Timer(functools.partial(sorted_lists, short1, short2))
rs1 = t.timeit(number)
print("sorted short :{}".format(rs1))
t = timeit.Timer(functools.partial(counted_lists, short1, short2))
rs2 = t.timeit(number)
print("counted short:{}".format(rs2)
The output is fairly consistent:
sorted long :0.04470205499092117 # less time = fastest
counted long :0.1182843999704346
sorted short :0.0012896459666080773 # less time = fastest
counted short:0.009829471004195511
Both sets of tests were run in python 3.6.