Dictionary bidirectional
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Mon Jul 14 07:48:22 EDT 2008
Larry Bates:
> The only case where it would be faster would be if most of the keys were NOT in
> the dictionary (rather odd use case). Otherwise I believe you will find the
> first way quicker as the exceptions are infrequent.
I have written a small benchmark:
from random import shuffle
def test1(data, to_delete):
for item in to_delete:
try:
del data[item]
except KeyError:
pass
def test2(data, to_delete):
for item in to_delete:
if item in data:
del data[item]
N = 1000000
M = 2 * N
data = dict.fromkeys(xrange(N), 0)
to_delete = range(M)
shuffle(to_delete)
from timeit import default_timer as clock
t = clock()
#test1(data, to_delete) # 2.4 s
test2(data, to_delete) # 0.8 s
print round(clock() - t, 2), "s"
It creates a dictionary of the first million integers, and then tries
to delete the first two million of integers. So about 1/2 numbers are
present to be deleted. In this situation the version with the try-
except seems about 3 times slower than the other.
Bye,
bearophile
More information about the Python-list
mailing list