<?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: If-else vs switch – Which is better?</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: AKriauciunas</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-14211</link>
		<dc:creator>AKriauciunas</dc:creator>
		<pubDate>Tue, 25 May 2010 21:09:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-14211</guid>
		<description>I didn&#039;t see anyone mention the underlying issue when dealing with compiled code - whenever you don&#039;t know for sure what the next statement is going to be (at the machine code level), the compiler will have to insert a few (typically 2-3) NOPs to flush the pipeline.  I believe the systems I worked on (Ti DSP processors) used 2-3 NOPs to do this.  So, all things being equal, every time you evaluate an if/else you have a few wasted instruction cycles.  The switch absorbs the overhead once when it looks up the jump offset in the jump table.  So, the real question is at which point does the variable overhead (flushing the pipeline every time you see an if statement) outweigh the slightly higher fixed overhead (computing the jump offset).  If your algorithm is on the critical path in a real-time environment, these are definitely important issues.</description>
		<content:encoded><![CDATA[<p>I didn&#8217;t see anyone mention the underlying issue when dealing with compiled code &#8211; whenever you don&#8217;t know for sure what the next statement is going to be (at the machine code level), the compiler will have to insert a few (typically 2-3) NOPs to flush the pipeline.  I believe the systems I worked on (Ti DSP processors) used 2-3 NOPs to do this.  So, all things being equal, every time you evaluate an if/else you have a few wasted instruction cycles.  The switch absorbs the overhead once when it looks up the jump offset in the jump table.  So, the real question is at which point does the variable overhead (flushing the pipeline every time you see an if statement) outweigh the slightly higher fixed overhead (computing the jump offset).  If your algorithm is on the critical path in a real-time environment, these are definitely important issues.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ksongking</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6673</link>
		<dc:creator>ksongking</dc:creator>
		<pubDate>Tue, 18 Aug 2009 02:21:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6673</guid>
		<description>Since many expects are here. I did try to benchmark in between if/else and switch case before. I agreed on switch case is faster than the if/else case, but normally the test is taking the avarage measurement for both statements to get matched. Last time when i did my test, i found another finding, i was using Bios/DSP and use the ADI provided compiler to benchmark the cycle counts for both statements, i found that, the switch statement is faster than the if/else statement. But, the time to match the first case in switch is the same with the time to match the last case, or the default case of the switch, so, regardless which case i matched in the switch statement, it will take the constant cycle times to run the who switch statement, and the cycle time is increasing if there are more case added in, my finding is, for switch statement, the time spent to match any cases is the same, there has no advantage even the first case is matched. Compare to the if/else statement, the last matching condition always take much than the switch, but if the condition always hit the first or second condition, then the cycle time reduced very much, so normally when using the if/else statement, we will prioritize the most likely happen case on top, this can really speed up the comparison compare to switch case since no matter which case it will be matched, the time taken will be constantly long, and it consume more MIPS when more cases added in. Please verify my finding.</description>
		<content:encoded><![CDATA[<p>Since many expects are here. I did try to benchmark in between if/else and switch case before. I agreed on switch case is faster than the if/else case, but normally the test is taking the avarage measurement for both statements to get matched. Last time when i did my test, i found another finding, i was using Bios/DSP and use the ADI provided compiler to benchmark the cycle counts for both statements, i found that, the switch statement is faster than the if/else statement. But, the time to match the first case in switch is the same with the time to match the last case, or the default case of the switch, so, regardless which case i matched in the switch statement, it will take the constant cycle times to run the who switch statement, and the cycle time is increasing if there are more case added in, my finding is, for switch statement, the time spent to match any cases is the same, there has no advantage even the first case is matched. Compare to the if/else statement, the last matching condition always take much than the switch, but if the condition always hit the first or second condition, then the cycle time reduced very much, so normally when using the if/else statement, we will prioritize the most likely happen case on top, this can really speed up the comparison compare to switch case since no matter which case it will be matched, the time taken will be constantly long, and it consume more MIPS when more cases added in. Please verify my finding.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ray</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6365</link>
		<dc:creator>Ray</dc:creator>
		<pubDate>Wed, 05 Aug 2009 15:47:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6365</guid>
		<description>The biggest benefit I tell junior programmers, in support of switch statement, is IDE support.  If you add an item to an Enumeration, many IDEs will warn you that it is not being used in your switch statement.  So the IDE is helping you ensure that your forking code is correct.  With if/else, you have to search through the code and hope you don&#039;t make a mistake.

So to me, this is a readability and maintenance issue.</description>
		<content:encoded><![CDATA[<p>The biggest benefit I tell junior programmers, in support of switch statement, is IDE support.  If you add an item to an Enumeration, many IDEs will warn you that it is not being used in your switch statement.  So the IDE is helping you ensure that your forking code is correct.  With if/else, you have to search through the code and hope you don&#8217;t make a mistake.</p>
<p>So to me, this is a readability and maintenance issue.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ashish</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6325</link>
		<dc:creator>ashish</dc:creator>
		<pubDate>Tue, 04 Aug 2009 14:52:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6325</guid>
		<description>Well Sysouts are present in both cases and its comparative. Since conditions are same, both emit output to console. So this was fine in my case.
Not sure what&#039;s next, right now hacking Apache Vysper code.</description>
		<content:encoded><![CDATA[<p>Well Sysouts are present in both cases and its comparative. Since conditions are same, both emit output to console. So this was fine in my case.<br />
Not sure what&#8217;s next, right now hacking Apache Vysper code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ashish</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6324</link>
		<dc:creator>ashish</dc:creator>
		<pubDate>Tue, 04 Aug 2009 14:49:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6324</guid>
		<description>Thanks Kirk! My simple objective was to evaluate my review comment &quot;Which is better&quot; in general context. Console IO is present for both. So the conditions are similar for both cases.</description>
		<content:encoded><![CDATA[<p>Thanks Kirk! My simple objective was to evaluate my review comment &#8220;Which is better&#8221; in general context. Console IO is present for both. So the conditions are similar for both cases.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ppow</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6317</link>
		<dc:creator>ppow</dc:creator>
		<pubDate>Tue, 04 Aug 2009 10:52:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6317</guid>
		<description>Why are you doing the benchmarking with System.out.printlns?? This is strongly affecting the results you are getting as this is a really time consuming operation. Maybe try to replace it with any other non-trivial opp (so that the compiler does not optimize your code and skip if-else or switch) or try to subtracts from the result the costs of System.out.println?

BTW: this is a really interesting topic, let me know about what you&#039;ll do next :)</description>
		<content:encoded><![CDATA[<p>Why are you doing the benchmarking with System.out.printlns?? This is strongly affecting the results you are getting as this is a really time consuming operation. Maybe try to replace it with any other non-trivial opp (so that the compiler does not optimize your code and skip if-else or switch) or try to subtracts from the result the costs of System.out.println?</p>
<p>BTW: this is a really interesting topic, let me know about what you&#8217;ll do next <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: scot</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6311</link>
		<dc:creator>scot</dc:creator>
		<pubDate>Tue, 04 Aug 2009 06:13:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6311</guid>
		<description>First, microbenchmarks are a waste of time.

Second, as Shaun says above, using either switch or if-else especially for more than the most trivial of cases is a very good indicator your code is in poor shape and requires redesigning. There is no &quot;better&quot; in this circumstance. Either way it is a pointer to a failure to apply the many well-known and well-proven techniques of object orientated design.</description>
		<content:encoded><![CDATA[<p>First, microbenchmarks are a waste of time.</p>
<p>Second, as Shaun says above, using either switch or if-else especially for more than the most trivial of cases is a very good indicator your code is in poor shape and requires redesigning. There is no &#8220;better&#8221; in this circumstance. Either way it is a pointer to a failure to apply the many well-known and well-proven techniques of object orientated design.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kirk</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6310</link>
		<dc:creator>kirk</dc:creator>
		<pubDate>Tue, 04 Aug 2009 05:49:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6310</guid>
		<description>I forgot to mention the timings are well within the range that you are measuring randomness. Any benchmark needs to be run for a significant period of time to dampen out the resolution of the clock and the cost of getting that measurement.</description>
		<content:encoded><![CDATA[<p>I forgot to mention the timings are well within the range that you are measuring randomness. Any benchmark needs to be run for a significant period of time to dampen out the resolution of the clock and the cost of getting that measurement.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kirk</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6309</link>
		<dc:creator>kirk</dc:creator>
		<pubDate>Tue, 04 Aug 2009 05:48:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6309</guid>
		<description>This bench is busted. First, get rid of the writing to console. It&#039;s activity will completely dominate the profile. Secondly, I&#039;d take a look at the byte code. I think you&#039;ll find that sparse switch and if-else get about the same byte code so there should be no difference. The range will be a calculated goto and so the result of the bench will be completely dependent on the ordering of the data.

There is no such thing as a simple microbenchmark.

Regards,
Kirk</description>
		<content:encoded><![CDATA[<p>This bench is busted. First, get rid of the writing to console. It&#8217;s activity will completely dominate the profile. Secondly, I&#8217;d take a look at the byte code. I think you&#8217;ll find that sparse switch and if-else get about the same byte code so there should be no difference. The range will be a calculated goto and so the result of the bench will be completely dependent on the ordering of the data.</p>
<p>There is no such thing as a simple microbenchmark.</p>
<p>Regards,<br />
Kirk</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ashish</title>
		<link>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/comment-page-1/#comment-6304</link>
		<dc:creator>ashish</dc:creator>
		<pubDate>Tue, 04 Aug 2009 01:08:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303#comment-6304</guid>
		<description>Thanks Shaun for the pointer. However, my objective here was very simple to see which is better. Most cases that I encounter, there are typically 5-8 conditions. Well, I just wanted to check that what I understand is really true under what circumstances.</description>
		<content:encoded><![CDATA[<p>Thanks Shaun for the pointer. However, my objective here was very simple to see which is better. Most cases that I encounter, there are typically 5-8 conditions. Well, I just wanted to check that what I understand is really true under what circumstances.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
