Is there a faster way to test if two lists have the exact same elements than Pythons built in == operator? -
if have 2 lists, each 800 elements long , filled integers. there faster way compare have exact same elements (and short circuit if don't) using built in ==
operator?
a = [6,2,3,88,54,-486] b = [6,2,3,88,54,-486] == b >>> true
anything better this?
i'm curious because have giant list of lists compare.
let's not assume, run tests!
the set-up:
>>> import time >>> def timeit(l1, l2, n): start = time.time() in xrange(n): l1 == l2 end = time.time() print "%d took %.2fs" % (n, end - start)
two giant equal lists:
>>> hugeequal1 = [10]*30000 >>> hugeequal2 = [10]*30000 >>> timeit(hugeequal1, hugeequal2, 10000) 10000 took 3.07s
two giant lists first element not equal:
>>> easydiff1 = [10]*30000 >>> easydiff2 = [10]*30000 >>> easydiff2[0] = 0 >>> timeit(easydiff1, easydiff2, 10000) 10000 took 0.00s >>> timeit(easydiff1, easydiff2, 1000000) 1000000 took 0.14s
so appears built-in list equality operator indeed short-circuiting.
edit: interestingly, using array.array
module doesn't make faster:
>>> import array >>> timeit(hugeequal1, hugeequal2, 1000) 1000 took 0.30s >>> timeit(array.array('l', hugeequal1), array.array('l', hugeequal2), 1000) 1000 took 1.11s
numpy
speed-up, though:
>>> import numpy >>> timeit(hugeequal1, hugeequal2, 10000) 10000 took 3.01s >>> timeit(numpy.array(hugeequal1), numpy.array(hugeequal2), 10000) 10000 took 1.11s
Comments
Post a Comment