Optimising list comparison

Sean 'Shaleh' Perry shalehperry at attbi.com
Fri Mar 15 18:14:23 EST 2002


> 
> This will run *significantly* faster if you pass list1 and list2 to
> compare() as arguments (access to function locals is much quicker than
> access to globals).  You could also exploit Python's chained comparisons:
> 

it shaved about a second off the run by passing in the lists in my tests.

> Then it's actually quite difficult to explain why
> 
> import operator
> def attempt5(list1, list2, sum=operator.__add__):
>     return 1 not in map(sum, list1, list2)
> 
> isn't quicker than an explicit loop doing the same thing.
> 

roughly 1.5 seconds slower than his best time.

> 
> Keep trying <wink>.  Also give
> 
>     for a, b in zip(list1, list2):
> 
> a shot.  It may surprise you <wink>.
> 

I tried this when he sent out his mail, roughly 2.5 seconds slower.

./loops.py 
100000 iterations of <function attempt1 at 0x80982d4> took: 8.464 secs
100000 iterations of <function attempt2 at 0x8098344> took: 20.863 secs
100000 iterations of <function attempt3 at 0x809837c> took: 8.747 secs
100000 iterations of <function attempt4 at 0x809853c> took: 10.957 secs
100000 iterations of <function attempt5 at 0x809a264> took: 9.921 secs
100000 iterations of <function attempt6 at 0x808be0c> took: 8.557 secs

you have seen 1 and 2 (they now pass in the lists as arguments).

3 uses xrange (thought not making a list would be faster), 4 uses zip, 5 is
Tim's map and 6 uses the == 1 optimization.

def attempt3(lista, listb):
    for i in xrange(len(lista)):
        if (lista[i] != listb[i]) and (lista[i] != -1) and (listb[i] != -1):
            return 0
    return 1

def attempt4(lista, listb):
    for (a,b) in zip(lista, listb):
        if (a != b) and (a != -1) and (b != -1):
            return 0
    return 1

import operator
def attempt5(lista, listb, sum=operator.__add__):
    return 1 not in map(sum, lista, listb)

def attempt6(lista, listb):
    for i in range(len(lista)):
        if lista[i] + listb[i] == 1: return 0
    return 1





More information about the Python-list mailing list