[Python-ideas] set.add(x) slower than if x in set:set.add(x)
Georg Brandl
g.brandl at gmx.net
Mon Sep 14 11:23:11 CEST 2009
Terry Reedy schrieb:
>> britton at TheBrittons:~$ python -m timeit -n 1000000 -s 'with
>> open("/usr/share/dict/words") as f: s = set(w.strip("\n") for w
>> in f)'
>> 'if "mother" not in s:s.add("mother")'
>> 1000000 loops, best of 3: 0.185 usec per loop
>
> Add 'sadd = s.add' at end of setup, followed by
> 'if "mother" not in s: sadd("mother")
>
> I doubt second will still be faster.
Well, the method also has to be *called* (think argument parsing, but in this
case that should be trivial).
Georg
--
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.
More information about the Python-ideas
mailing list