<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Remember your in operator</title>
	<atom:link href="http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/</link>
	<description>A Finn in Silicon Valley - Adventures in Technology</description>
	<lastBuildDate>Wed, 17 Aug 2011 08:58:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
	<item>
		<title>By: Ernie</title>
		<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/comment-page-1/#comment-14759</link>
		<dc:creator>Ernie</dc:creator>
		<pubDate>Wed, 12 Aug 2009 09:19:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=844#comment-14759</guid>
		<description>@lorg

if you know enough about the logic of your program to hardcode a==1 or a==7 or a==15, then you know enough to hardcode set([1,7,15]) outside the loop.

Creating the set takes ~6 times longer than doing one hardcoded comparison, but further comparisons are quite a bit faster.  I think the choice of what to use depends on how many times you&#039;ll execute the statement.  But there should never be a case you create the set repeatedly.</description>
		<content:encoded><![CDATA[<p>@lorg</p>
<p>if you know enough about the logic of your program to hardcode a==1 or a==7 or a==15, then you know enough to hardcode set([1,7,15]) outside the loop.</p>
<p>Creating the set takes ~6 times longer than doing one hardcoded comparison, but further comparisons are quite a bit faster.  I think the choice of what to use depends on how many times you&#8217;ll execute the statement.  But there should never be a case you create the set repeatedly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lorg</title>
		<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/comment-page-1/#comment-14757</link>
		<dc:creator>lorg</dc:creator>
		<pubDate>Wed, 12 Aug 2009 06:55:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=844#comment-14757</guid>
		<description>@Ernie:
Note that your comparison isn&#039;t really fair, as you don&#039;t take into account the time spent building the set.
Since usually you won&#039;t reuse the set for other comparisons, and the idiom is &quot;a in set([...])&quot;, you should take it into account. See:
In [2]: a = 16
In [3]: timeit a==1 or a==7 or a==16
10000000 loops, best of 3: 144 ns per loop
In [4]: timeit a in set([1,7,15])
1000000 loops, best of 3: 786 ns per loop</description>
		<content:encoded><![CDATA[<p>@Ernie:<br />
Note that your comparison isn&#8217;t really fair, as you don&#8217;t take into account the time spent building the set.<br />
Since usually you won&#8217;t reuse the set for other comparisons, and the idiom is &#8220;a in set([...])&#8221;, you should take it into account. See:<br />
In [2]: a = 16<br />
In [3]: timeit a==1 or a==7 or a==16<br />
10000000 loops, best of 3: 144 ns per loop<br />
In [4]: timeit a in set([1,7,15])<br />
1000000 loops, best of 3: 786 ns per loop</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ernie</title>
		<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/comment-page-1/#comment-14750</link>
		<dc:creator>Ernie</dc:creator>
		<pubDate>Wed, 12 Aug 2009 04:15:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=844#comment-14750</guid>
		<description>Using set is faster than using 3 comparisons.  So using in looks best and is fastest.

In [1]: a=16

In [2]: s=set([1,7,15])

In [3]: timeit a==1 or a==7 or a==16
10000000 loops, best of 3: 126 ns per loop

In [4]: timeit a in s
10000000 loops, best of 3: 107 ns per loop</description>
		<content:encoded><![CDATA[<p>Using set is faster than using 3 comparisons.  So using in looks best and is fastest.</p>
<p>In [1]: a=16</p>
<p>In [2]: s=set([1,7,15])</p>
<p>In [3]: timeit a==1 or a==7 or a==16<br />
10000000 loops, best of 3: 126 ns per loop</p>
<p>In [4]: timeit a in s<br />
10000000 loops, best of 3: 107 ns per loop</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ssadler</title>
		<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/comment-page-1/#comment-14732</link>
		<dc:creator>ssadler</dc:creator>
		<pubDate>Tue, 11 Aug 2009 21:44:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=844#comment-14732</guid>
		<description>@tdrusk: I don&#039;t think it really improves readability to do it for one number.</description>
		<content:encoded><![CDATA[<p>@tdrusk: I don&#8217;t think it really improves readability to do it for one number.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tdrusk</title>
		<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/comment-page-1/#comment-14731</link>
		<dc:creator>tdrusk</dc:creator>
		<pubDate>Tue, 11 Aug 2009 21:35:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=844#comment-14731</guid>
		<description>Cool. I am just getting used to Python. I am starting to get that tingly feeling you described. 

It took me a minute to figure out what you were doing with the list, but after reading a comment or two I realized that I had to do this for one number.
x=10
if x in (10,):
    print &#039;hello&#039;

I suppose there&#039;s more than one way to skin a cat.</description>
		<content:encoded><![CDATA[<p>Cool. I am just getting used to Python. I am starting to get that tingly feeling you described. </p>
<p>It took me a minute to figure out what you were doing with the list, but after reading a comment or two I realized that I had to do this for one number.<br />
x=10<br />
if x in (10,):<br />
    print &#8216;hello&#8217;</p>
<p>I suppose there&#8217;s more than one way to skin a cat.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tasc</title>
		<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/comment-page-1/#comment-14728</link>
		<dc:creator>tasc</dc:creator>
		<pubDate>Tue, 11 Aug 2009 20:36:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=844#comment-14728</guid>
		<description>sets are hashes, in is a O(1) operation</description>
		<content:encoded><![CDATA[<p>sets are hashes, in is a O(1) operation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ssadler</title>
		<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/comment-page-1/#comment-14725</link>
		<dc:creator>ssadler</dc:creator>
		<pubDate>Tue, 11 Aug 2009 20:12:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=844#comment-14725</guid>
		<description>@tasc, why is that? Are sets more efficient for this operation?</description>
		<content:encoded><![CDATA[<p>@tasc, why is that? Are sets more efficient for this operation?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tasc</title>
		<link>http://www.heikkitoivonen.net/blog/2009/06/16/remember-your-in-operator/comment-page-1/#comment-14724</link>
		<dc:creator>tasc</dc:creator>
		<pubDate>Tue, 11 Aug 2009 19:43:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=844#comment-14724</guid>
		<description>@Armin: in this particular case set would be more appropriate.</description>
		<content:encoded><![CDATA[<p>@Armin: in this particular case set would be more appropriate.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

