<?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: Large if block vs list lookup</title>
	<atom:link href="http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/feed/" rel="self" type="application/rss+xml" />
	<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/</link>
	<description>Various ramblings-on, mostly about Red5</description>
	<lastBuildDate>Tue, 31 Aug 2010 17:12:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: mondain</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8712</link>
		<dc:creator>mondain</dc:creator>
		<pubDate>Sat, 21 Nov 2009 23:51:36 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8712</guid>
		<description>Actually valueOf only works if the strings match the enum name like so: 
CONNECT == CONNECT
CONNECT != connect</description>
		<content:encoded><![CDATA[<p>Actually valueOf only works if the strings match the enum name like so:<br />
CONNECT == CONNECT<br />
CONNECT != connect</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mondain</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8708</link>
		<dc:creator>mondain</dc:creator>
		<pubDate>Sat, 21 Nov 2009 22:48:59 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8708</guid>
		<description>@annorax using string enums in a switch like you describe doesnt working Java 1.5/1.6 but should in 1.7. This in-fact will work:
				switch(StreamAction.valueOf(action)) {
    case CREATE_STREAM:
    case DELETE_STREAM:
        //do something
}</description>
		<content:encoded><![CDATA[<p>@annorax using string enums in a switch like you describe doesnt working Java 1.5/1.6 but should in 1.7. This in-fact will work:<br />
				switch(StreamAction.valueOf(action)) {<br />
    case CREATE_STREAM:<br />
    case DELETE_STREAM:<br />
        //do something<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Z.D.</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8700</link>
		<dc:creator>Z.D.</dc:creator>
		<pubDate>Fri, 20 Nov 2009 22:27:34 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8700</guid>
		<description>agree with annorax, use enum! And java can optimize switch block o(1) then the linear search an ArrayList.contains has!</description>
		<content:encoded><![CDATA[<p>agree with annorax, use enum! And java can optimize switch block o(1) then the linear search an ArrayList.contains has!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benji Smith</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8363</link>
		<dc:creator>Benji Smith</dc:creator>
		<pubDate>Mon, 10 Aug 2009 23:11:32 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8363</guid>
		<description>Previous comment blocked by spam filter? Bummer.

The gyst of it was that you shouldn&#039;t use a List[T] for containment-lookup with a long list of elements, since the execution time will be proportional to the size of the list (and will require creation of an iterator.

Instead, use a Set[T], which has constant-time performance and doesn&#039;t create a new iterator for every containment-lookup.</description>
		<content:encoded><![CDATA[<p>Previous comment blocked by spam filter? Bummer.</p>
<p>The gyst of it was that you shouldn&#8217;t use a List[T] for containment-lookup with a long list of elements, since the execution time will be proportional to the size of the list (and will require creation of an iterator.</p>
<p>Instead, use a Set[T], which has constant-time performance and doesn&#8217;t create a new iterator for every containment-lookup.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benji Smith</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8362</link>
		<dc:creator>Benji Smith</dc:creator>
		<pubDate>Mon, 10 Aug 2009 19:37:02 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8362</guid>
		<description>Don&#039;t use that list-based code!!

The ArrayList.contains() method will create an iterator and then walk the entire collection to find the value. Although the code looks nicer than the huge block of if/else statements, the performance won&#039;t be any better. It&#039;s fundamentally an O(n) algorithm.

Instead, create a HashSet[String].

The HashSet.contains method doesn&#039;t create an iterator. And it doesn&#039;t perform comparisons against every value in the collection. Instead of suffering through O(n) performance, you&#039;ll enjoy O(1) performance. Hooray!

Here&#039;s the code:

public static final Set[String] STREAM_ACTION_SET = new HashSet[String](11)
   {
      add(ACTION_CREATE_STREAM);
      add(ACTION_DELETE_STREAM);
      add(ACTION_RELEASE_STREAM);
      add(ACTION_PUBLISH);
      add(ACTION_PLAY);
      add(ACTION_SEEK);
      add(ACTION_PAUSE);
      add(ACTION_PAUSE_RAW);
      add(ACTION_CLOSE_STREAM);
      add(ACTION_RECEIVE_VIDEO);
      add(ACTION_RECEIVE_AUDIO);
   }
};

// ...

if (STREAM_ACTION_SET.contains(action)) {
   // LARGE SETS ARE FASTER THAN LARGE LISTS
   // FOR CHECKING VALUE CONTAINMENT!!!!!!
}</description>
		<content:encoded><![CDATA[<p>Don&#8217;t use that list-based code!!</p>
<p>The ArrayList.contains() method will create an iterator and then walk the entire collection to find the value. Although the code looks nicer than the huge block of if/else statements, the performance won&#8217;t be any better. It&#8217;s fundamentally an O(n) algorithm.</p>
<p>Instead, create a HashSet[String].</p>
<p>The HashSet.contains method doesn&#8217;t create an iterator. And it doesn&#8217;t perform comparisons against every value in the collection. Instead of suffering through O(n) performance, you&#8217;ll enjoy O(1) performance. Hooray!</p>
<p>Here&#8217;s the code:</p>
<p>public static final Set[String] STREAM_ACTION_SET = new HashSet[String](11)<br />
   {<br />
      add(ACTION_CREATE_STREAM);<br />
      add(ACTION_DELETE_STREAM);<br />
      add(ACTION_RELEASE_STREAM);<br />
      add(ACTION_PUBLISH);<br />
      add(ACTION_PLAY);<br />
      add(ACTION_SEEK);<br />
      add(ACTION_PAUSE);<br />
      add(ACTION_PAUSE_RAW);<br />
      add(ACTION_CLOSE_STREAM);<br />
      add(ACTION_RECEIVE_VIDEO);<br />
      add(ACTION_RECEIVE_AUDIO);<br />
   }<br />
};</p>
<p>// &#8230;</p>
<p>if (STREAM_ACTION_SET.contains(action)) {<br />
   // LARGE SETS ARE FASTER THAN LARGE LISTS<br />
   // FOR CHECKING VALUE CONTAINMENT!!!!!!<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: annorax</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8361</link>
		<dc:creator>annorax</dc:creator>
		<pubDate>Mon, 10 Aug 2009 00:53:17 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8361</guid>
		<description>A note about enum naming - a better name for this enum would be StreamAction, rather than StreamActions.

Enum naming always confuses me, since I tend to think of enums as collections. However, much like classes, enums are actually types, whereas their values (e.g. ACTION_CREATE_STREAM etc.) are equivalent to instances of that type.

Therefore, it is always best to use singular names rather than plural, as in the examples given by Sun in the introductory docs (http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html).</description>
		<content:encoded><![CDATA[<p>A note about enum naming &#8211; a better name for this enum would be StreamAction, rather than StreamActions.</p>
<p>Enum naming always confuses me, since I tend to think of enums as collections. However, much like classes, enums are actually types, whereas their values (e.g. ACTION_CREATE_STREAM etc.) are equivalent to instances of that type.</p>
<p>Therefore, it is always best to use singular names rather than plural, as in the examples given by Sun in the introductory docs (<a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/java.sun.com/j2se/1.5.0/docs/guide/language/enums.html?referer=');">http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html</a>).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mondain</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8359</link>
		<dc:creator>mondain</dc:creator>
		<pubDate>Sun, 09 Aug 2009 06:43:27 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8359</guid>
		<description>@annorax nice one! I&#039;m sure we could replace quite a number of string constants in the server with enums, maybe I&#039;ll get started on that...</description>
		<content:encoded><![CDATA[<p>@annorax nice one! I&#8217;m sure we could replace quite a number of string constants in the server with enums, maybe I&#8217;ll get started on that&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: annorax</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8358</link>
		<dc:creator>annorax</dc:creator>
		<pubDate>Sat, 08 Aug 2009 23:56:29 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8358</guid>
		<description>If you&#039;re using Java 5.0 or 6.0 to compile, a more elegant solution would be to create an enum instead of all these unruly final Strings. Then you can use a switch.

The enum should look like this:

public enum StreamActions {
	ACTION_CREATE_STREAM(&quot;string1&quot;),
	ACTION_DELETE_STREAM(&quot;string2&quot;),
	ACTION_RELEASE_STREAM(&quot;string3&quot;),
	ACTION_PUBLISH(&quot;string4&quot;),
	ACTION_PLAY(&quot;string5&quot;),
	ACTION_SEEK(&quot;string6&quot;),
	ACTION_PAUSE(&quot;string7&quot;),
	ACTION_PAUSE_RAW(&quot;string8&quot;),
	ACTION_CLOSE_STREAM(&quot;string9&quot;),
	ACTION_RECEIVE_VIDEO(&quot;string10&quot;),
	ACTION_RECEIVE_AUDIO(&quot;string11&quot;);

	protected String actionString;

	StreamActions(String actionString) {
		this.actionString = actionString;
	}

	public String getString() {
		return actionString;
	}
}

Your if statement can then be replaced with:

switch(action) {
	case ACTION_CREATE_STREAM:
	case ACTION_DELETE_STREAM:
	case ACTION_RELEASE_STREAM:
	case ACTION_PUBLISH:
	case ACTION_PLAY:
	case ACTION_SEEK:
	case ACTION_PAUSE:
	case ACTION_PAUSE_RAW:
	case ACTION_CLOSE_STREAM:
	case ACTION_RECEIVE_VIDEO:
	case ACTION_RECEIVE_AUDIO:
		//do something
}</description>
		<content:encoded><![CDATA[<p>If you&#8217;re using Java 5.0 or 6.0 to compile, a more elegant solution would be to create an enum instead of all these unruly final Strings. Then you can use a switch.</p>
<p>The enum should look like this:</p>
<p>public enum StreamActions {<br />
	ACTION_CREATE_STREAM(&#8220;string1&#8243;),<br />
	ACTION_DELETE_STREAM(&#8220;string2&#8243;),<br />
	ACTION_RELEASE_STREAM(&#8220;string3&#8243;),<br />
	ACTION_PUBLISH(&#8220;string4&#8243;),<br />
	ACTION_PLAY(&#8220;string5&#8243;),<br />
	ACTION_SEEK(&#8220;string6&#8243;),<br />
	ACTION_PAUSE(&#8220;string7&#8243;),<br />
	ACTION_PAUSE_RAW(&#8220;string8&#8243;),<br />
	ACTION_CLOSE_STREAM(&#8220;string9&#8243;),<br />
	ACTION_RECEIVE_VIDEO(&#8220;string10&#8243;),<br />
	ACTION_RECEIVE_AUDIO(&#8220;string11&#8243;);</p>
<p>	protected String actionString;</p>
<p>	StreamActions(String actionString) {<br />
		this.actionString = actionString;<br />
	}</p>
<p>	public String getString() {<br />
		return actionString;<br />
	}<br />
}</p>
<p>Your if statement can then be replaced with:</p>
<p>switch(action) {<br />
	case ACTION_CREATE_STREAM:<br />
	case ACTION_DELETE_STREAM:<br />
	case ACTION_RELEASE_STREAM:<br />
	case ACTION_PUBLISH:<br />
	case ACTION_PLAY:<br />
	case ACTION_SEEK:<br />
	case ACTION_PAUSE:<br />
	case ACTION_PAUSE_RAW:<br />
	case ACTION_CLOSE_STREAM:<br />
	case ACTION_RECEIVE_VIDEO:<br />
	case ACTION_RECEIVE_AUDIO:<br />
		//do something<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mondain</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8357</link>
		<dc:creator>mondain</dc:creator>
		<pubDate>Sat, 08 Aug 2009 22:15:22 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8357</guid>
		<description>I &quot;plan&quot; to write a test for this :)</description>
		<content:encoded><![CDATA[<p>I &#8220;plan&#8221; to write a test for this <img src='http://gregoire.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Art Clarke</title>
		<link>http://gregoire.org/2009/08/08/large-if-block-vs-list-lookup/comment-page-1/#comment-8356</link>
		<dc:creator>Art Clarke</dc:creator>
		<pubDate>Sat, 08 Aug 2009 20:54:47 +0000</pubDate>
		<guid isPermaLink="false">http://gregoire.org/?p=59#comment-8356</guid>
		<description>It definitely makes for cleaner code, and I agree that 99 out of 100 times, the list based code should be used.

However, I just had to go through some code on something weŕe working on and move everything back to the old form, because the code in question was so hot, that the implicit creation, use and destruction of a Java iterator in the ArrayList.contains(...) method was a performance bottleneck for us.

Now, like I said, 99 out of 100 times, the ArrayList method is better.  But do measure :)</description>
		<content:encoded><![CDATA[<p>It definitely makes for cleaner code, and I agree that 99 out of 100 times, the list based code should be used.</p>
<p>However, I just had to go through some code on something weŕe working on and move everything back to the old form, because the code in question was so hot, that the implicit creation, use and destruction of a Java iterator in the ArrayList.contains(&#8230;) method was a performance bottleneck for us.</p>
<p>Now, like I said, 99 out of 100 times, the ArrayList method is better.  But do measure <img src='http://gregoire.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
