Most efficient solution?

Alex Martelli aleaxit at yahoo.com
Mon Jul 16 11:46:56 EDT 2001


"Bernhard Herzog" <bh at intevation.de> wrote in message
news:6qae252b15.fsf at abnoba.intevation.de...
    ...
> A bit more elegant, perhaps, and a little faster still would be to use 1
> as the value in C and directly use C.get in filter:
>
> C = {}
> for item in B:
>     C[item] = 1
>
> A = filter(C.get, A)

Actually, on my box, has_key appears to be a TINY little
bit faster...:

import time

B = range(7,77)
C = {}
for item in B:
    C[item] = 1

A = range(777)
start = time.clock()
for i in range(10000):
    A = filter(C.get, A)
stend = time.clock()
print "get: %6.2f"%(stend-start)

A = range(777)
start = time.clock()
for i in range(10000):
    A = filter(C.has_key, A)
stend = time.clock()
print "has: %6.2f"%(stend-start)

D:\>python timit.py
get:   2.33
has:   1.98

I.e., about 15% faster for has_key, it seems.


Alex






More information about the Python-list mailing list