<?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: Optimizing Switch-Case Statements In C For Speed</title>
	<atom:link href="http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html</link>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Tue, 31 Aug 2010 16:47:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Greg Smith</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-3515</link>
		<dc:creator>Greg Smith</dc:creator>
		<pubDate>Wed, 02 Jun 2010 21:37:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-3515</guid>
		<description>Will just add this: In cases where compilers do not generate jump tables (due to cases too few or too sparse) most compilers I&#039;ve looked into will not generate a one-by-one search, instead they will start by comparing to a number numerically near the middle of the list, and branch differently for greater or less; basically they build a fixed &#039;binary tree&#039; search, so the number of compare to find the right case will be O(log(n)). In that case it won&#039;t matter what order you put them in.

@Shantanu &quot;gcc 3.x series can’t even create a good jump table for small switch case statement&quot;. How do you know the jump table is going to be better? if 5 or 6 cases are handled by a few compares, and you have branch prediction hardware in the processor, that could easily be better than the jump table (which always takes an unpredictable branch, plus a data-dependent memory read). Look at this way, if they create one for a big list, and not for a small list, that was a deliberate decision.</description>
		<content:encoded><![CDATA[<p>Will just add this: In cases where compilers do not generate jump tables (due to cases too few or too sparse) most compilers I&#8217;ve looked into will not generate a one-by-one search, instead they will start by comparing to a number numerically near the middle of the list, and branch differently for greater or less; basically they build a fixed &#8216;binary tree&#8217; search, so the number of compare to find the right case will be O(log(n)). In that case it won&#8217;t matter what order you put them in.</p>
<p>@Shantanu &#8220;gcc 3.x series can’t even create a good jump table for small switch case statement&#8221;. How do you know the jump table is going to be better? if 5 or 6 cases are handled by a few compares, and you have branch prediction hardware in the processor, that could easily be better than the jump table (which always takes an unpredictable branch, plus a data-dependent memory read). Look at this way, if they create one for a big list, and not for a small list, that was a deliberate decision.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zombie No. 5</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-670</link>
		<dc:creator>Zombie No. 5</dc:creator>
		<pubDate>Sun, 05 Apr 2009 17:32:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-670</guid>
		<description>Rather than optimizing for speed, you should consider optimizing for safety and correctness. Whenever it makes sense, for example, when implementing a finite-state machine consider enumerators instead of integers. A good compiler like GCC will warn you about unhandled cases. Of course, that means you have to omit the &quot;default:&quot; label. You should still catch unhandled values like in the example below:

enum my_enum { a, b, c };

int
function(enum my_enum value)
{
  switch (value) {
  case a: return ...;
  case b: return ...;
  case c: return ...;
  }
/* handle unexpected case */
}

Of course you can also use goto or a flag, if use of return as above isn&#039;t suitable.

By the way, a compiler can only generate a jump table if it can deduce that the switch parameter or the label values are from a limited range. A jump-table with UINT_MAX entries would be hardly efficient or desirable.</description>
		<content:encoded><![CDATA[<p>Rather than optimizing for speed, you should consider optimizing for safety and correctness. Whenever it makes sense, for example, when implementing a finite-state machine consider enumerators instead of integers. A good compiler like GCC will warn you about unhandled cases. Of course, that means you have to omit the &#8220;default:&#8221; label. You should still catch unhandled values like in the example below:</p>
<p>enum my_enum { a, b, c };</p>
<p>int<br />
function(enum my_enum value)<br />
{<br />
  switch (value) {<br />
  case a: return &#8230;;<br />
  case b: return &#8230;;<br />
  case c: return &#8230;;<br />
  }<br />
/* handle unexpected case */<br />
}</p>
<p>Of course you can also use goto or a flag, if use of return as above isn&#8217;t suitable.</p>
<p>By the way, a compiler can only generate a jump table if it can deduce that the switch parameter or the label values are from a limited range. A jump-table with UINT_MAX entries would be hardly efficient or desirable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: compiler</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-100</link>
		<dc:creator>compiler</dc:creator>
		<pubDate>Fri, 06 Feb 2009 06:23:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-100</guid>
		<description>I also agree that the micro optimization is not always useful. There are lots of code patterns which can be observe by a compiler and can be optimized by compiler.  But compilers do not implement all of these patterns and only observe most common code patterns. So it is batter to write the simple code without micro-optimization (which may be optimized for one processor but not for other), and let compiler do the optimization.</description>
		<content:encoded><![CDATA[<p>I also agree that the micro optimization is not always useful. There are lots of code patterns which can be observe by a compiler and can be optimized by compiler.  But compilers do not implement all of these patterns and only observe most common code patterns. So it is batter to write the simple code without micro-optimization (which may be optimized for one processor but not for other), and let compiler do the optimization.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David F. Skoll</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-91</link>
		<dc:creator>David F. Skoll</dc:creator>
		<pubDate>Mon, 02 Feb 2009 11:48:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-91</guid>
		<description>What a waste of time.  Micro-optimizations are almost never helpful.  Unless you know for sure the assembly code produced by the compiler, you&#039;re just guessing.  And even if you do examine the generated code... congratulations!  You&#039;ve wasted time micro-optimizing for *one* compiler on *one* processor architecture.</description>
		<content:encoded><![CDATA[<p>What a waste of time.  Micro-optimizations are almost never helpful.  Unless you know for sure the assembly code produced by the compiler, you&#8217;re just guessing.  And even if you do examine the generated code&#8230; congratulations!  You&#8217;ve wasted time micro-optimizing for *one* compiler on *one* processor architecture.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-90</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Mon, 02 Feb 2009 10:39:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-90</guid>
		<description>@Ron: Thanks. You are absolutely correct in your observation that other issues/optimizations are worth more time than switch-case optimization. However, we&#039;ve started this blog to capture as much as we can about these topics, however obscure it might be. So, you will see us covering this as well as all other aspects of optimization, security, etc that make up the base for a solid code. And I hope that readers like you, daniel, dummy, andre and others will keep us on our toes and correct us wherever we deviate from the right path. Thanks once again. :)</description>
		<content:encoded><![CDATA[<p>@Ron: Thanks. You are absolutely correct in your observation that other issues/optimizations are worth more time than switch-case optimization. However, we&#8217;ve started this blog to capture as much as we can about these topics, however obscure it might be. So, you will see us covering this as well as all other aspects of optimization, security, etc that make up the base for a solid code. And I hope that readers like you, daniel, dummy, andre and others will keep us on our toes and correct us wherever we deviate from the right path. Thanks once again. <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ron</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-89</link>
		<dc:creator>Ron</dc:creator>
		<pubDate>Mon, 02 Feb 2009 07:36:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-89</guid>
		<description>Well,

given all the other common errors that are made, this case optimizing is something that is needed only in very specific cases. You normally would only need this if you want to optimize a CPU intensive task where the case statement is the bottleneck. Seems pretty far-stretched to me. 
In 20 years of systems programming (in C) I don&#039;t remember ever actually needing to optimize a case statement. 
Also, if ever you encounter such a situation anyway, then #2 might prove the most useful advice.</description>
		<content:encoded><![CDATA[<p>Well,</p>
<p>given all the other common errors that are made, this case optimizing is something that is needed only in very specific cases. You normally would only need this if you want to optimize a CPU intensive task where the case statement is the bottleneck. Seems pretty far-stretched to me.<br />
In 20 years of systems programming (in C) I don&#8217;t remember ever actually needing to optimize a case statement.<br />
Also, if ever you encounter such a situation anyway, then #2 might prove the most useful advice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-88</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Mon, 02 Feb 2009 06:11:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-88</guid>
		<description>@daniel: Your assumption that a modern compiler will always generate the jump-table isn&#039;t correct. Just write some code according to the limitations I&#039;ve mentioned and assemble it to see the same. A compiler can goof up as well if not given enough pointers. Heck, gcc 3.x series can&#039;t even create a good jump table for small switch case statements, leave apart the rest. Optimization of code is still a very relevant topic, especially if you are writing code for embedded devices. Moreover, not all compilers are good at optimization. If you have noticed, there are thousands of compilers out there in the market, many of which need to be used even if they are not that good at optimization.</description>
		<content:encoded><![CDATA[<p>@daniel: Your assumption that a modern compiler will always generate the jump-table isn&#8217;t correct. Just write some code according to the limitations I&#8217;ve mentioned and assemble it to see the same. A compiler can goof up as well if not given enough pointers. Heck, gcc 3.x series can&#8217;t even create a good jump table for small switch case statements, leave apart the rest. Optimization of code is still a very relevant topic, especially if you are writing code for embedded devices. Moreover, not all compilers are good at optimization. If you have noticed, there are thousands of compilers out there in the market, many of which need to be used even if they are not that good at optimization.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: daniel</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-87</link>
		<dc:creator>daniel</dc:creator>
		<pubDate>Mon, 02 Feb 2009 05:25:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-87</guid>
		<description>1. actually, most modern/decent compilers will generate the &quot;jump-table&quot;. this renders &quot;optimization technique 1&quot; irrelevant.


2. &quot;optimization technique 2&quot; is also about weak (realy non-optimizing compilers) which fail to generate the jump-table. so, given the fact that the array is calculated by the compiler at compile time, then, of course, like any array, has a O(1), this &quot;optimization technique&quot; is also irrelevant.


3. &quot;optimization technique 3&quot; is also a patch for poorly implemented compilers. in almost 40 years of c/c++, i&#039;ve never seen something so weird.


besides, the whole thing (the whole idea of explicitly &quot;optimizing&quot; your code instead of using a decent compiler) is in lala land... (sounds like the late 60s, maybe early 70s).


like the author said &quot;this was it for today&quot;. quick reminder, we&#039;re in 2009... just in case some of us haven&#039;t noticed... :-)


regards,
d</description>
		<content:encoded><![CDATA[<p>1. actually, most modern/decent compilers will generate the &#8220;jump-table&#8221;. this renders &#8220;optimization technique 1&#8243; irrelevant.</p>
<p>2. &#8220;optimization technique 2&#8243; is also about weak (realy non-optimizing compilers) which fail to generate the jump-table. so, given the fact that the array is calculated by the compiler at compile time, then, of course, like any array, has a O(1), this &#8220;optimization technique&#8221; is also irrelevant.</p>
<p>3. &#8220;optimization technique 3&#8243; is also a patch for poorly implemented compilers. in almost 40 years of c/c++, i&#8217;ve never seen something so weird.</p>
<p>besides, the whole thing (the whole idea of explicitly &#8220;optimizing&#8221; your code instead of using a decent compiler) is in lala land&#8230; (sounds like the late 60s, maybe early 70s).</p>
<p>like the author said &#8220;this was it for today&#8221;. quick reminder, we&#8217;re in 2009&#8230; just in case some of us haven&#8217;t noticed&#8230; <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>regards,<br />
d</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-86</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Mon, 02 Feb 2009 05:24:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-86</guid>
		<description>@Dummy00001: The lookup table is indeed a good option. Thanks for reminding us.</description>
		<content:encoded><![CDATA[<p>@Dummy00001: The lookup table is indeed a good option. Thanks for reminding us.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</title>
		<link>http://www.safercode.com/blog/2008/10/28/optimizing-switch-case-statements-in-c-for-speed.html#comment-85</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Mon, 02 Feb 2009 05:22:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=17#comment-85</guid>
		<description>@Andre Goddard Rosa: Yes, Duff&#039;s device discussion is in the offing but in another context (loop unrolling) which is being taken care of in the latest series. Check out our latest post from Jan 27th. :)</description>
		<content:encoded><![CDATA[<p>@Andre Goddard Rosa: Yes, Duff&#8217;s device discussion is in the offing but in another context (loop unrolling) which is being taken care of in the latest series. Check out our latest post from Jan 27th. <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
