Performance: sets vs dicts.

Peter Otten __peter__ at web.de
Sun Aug 29 15:43:02 EDT 2010


John Nagle wrote:

>     Is the "in" test faster for a dict or a set?
> Is "frozenset" faster than "set"?  Use case is
> for things like applying "in" on a list of 500 or so words
> while checking a large body of text.

As Arnaud suspects: no significant difference:

$ python dictperf.py
dict --> 0.210289001465
set --> 0.202902793884
frozenset --> 0.198950052261

$ cat dictperf.py
import random
import timeit

with open("/usr/share/dict/words") as instream:
    words = [line.strip() for line in instream]

#random.seed(42)
sample = random.sample(words, 501)

n = sample.pop()
y = random.choice(sample)

d = dict.fromkeys(sample)
s = set(sample)
f = frozenset(sample)


for lookup in d, s, f:
    print type(lookup).__name__, "-->", timeit.timeit(
        "n in lookup; y in lookup",
        "from __main__ import lookup, n, y")

Peter



More information about the Python-list mailing list