[Python-ideas] set.add(x) slower than if x in set:set.add(x)

Yuvgoog Greenle ubershmekel at gmail.com
Mon Sep 14 04:56:05 CEST 2009


So this pattern is a valid python optimization? Funky...
Sadly, there's no way around it unless the interpreter somehow did it
magically for you.

On Sun, Sep 13, 2009 at 10:37 PM, Raymond Hettinger <python at rcn.com> wrote:

>
> On Sep 13, 2009, at 11:10 AM, Gerald Britton wrote:
>
>>  Here are some results:
>>
>> $ python -m timeit -n 1000000  -s 'with open("/usr/share/dict/words")
>> as f: s = set(w.strip("\n") for w in f)'  's.add("mother")'
>> 1000000 loops, best of 3: 0.292 usec per loop
>>
>> 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
>>
>> the second example beats the first by about 36%
>>
>> Is the timing difference just the cost of the method lookup for s.add,
>> or is something else happening that I'm not seeing?
>>
>
> It is the something else you're not seeing ;-)
>
> On the first pass of the 1000000 loops, "mother" gets added.
> On the remaining passes the 'if "mother" not in set' test fails
> and the set.add() never gets executed.  That latter operation
> is a bit more expensive than the contains-test because it
> includes the time to lookup and bind the add method.
>
>
> Raymond
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090914/1917f8e2/attachment.html>


More information about the Python-ideas mailing list