<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DaVinci Unlimited Software &#187; disassembly</title>
	<atom:link href="http://www.davinciunltd.com/tag/disassembly/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davinciunltd.com</link>
	<description>Jim McKeeth's blog on creative and innovative Delphi programming.</description>
	<lastBuildDate>Wed, 09 Nov 2011 10:39:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Order of Enum in Case Statement</title>
		<link>http://www.davinciunltd.com/2008/07/order-of-enum-in-case-statement/</link>
		<comments>http://www.davinciunltd.com/2008/07/order-of-enum-in-case-statement/#comments</comments>
		<pubDate>Sun, 13 Jul 2008 00:16:58 +0000</pubDate>
		<dc:creator>Jim McKeeth</dc:creator>
				<category><![CDATA[Delphi]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[disassembly]]></category>
		<category><![CDATA[Enumerations]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://www.davinciunltd.com/2008/07/order-of-enum-in-case-statement/</guid>
		<description><![CDATA[A while back my manager asked me if the order of the enums in a Delphi case statement changed performance: i.e. Enums in order being faster then those not.&#160; I was pretty sure it didn&#8217;t, but thought it was worth checking out.&#160; Time for a test application and some disassembly . . . 

Here are [...]]]></description>
			<content:encoded><![CDATA[<p>A while back my manager asked me if the order of the enums in a Delphi case statement changed performance: i.e. Enums in order being faster then those not.&#160; I was pretty sure it didn&#8217;t, but thought it was worth checking out.&#160; Time for a test application and some disassembly . . . </p>
<p><span id="more-72"></span></p>
<p>Here are my types and variables, which are the same for both examples.</p>
<pre><code><strong>type</strong></code>
  TMyEnum = (my1, my2, my3, my4);</pre>
<pre><code class="keyword"><strong>var</strong></code>
  myEnum: TMyEnum;
  val: Char;</pre>
<p>Here are the two examples, side by side, with assembly code to follow.&#160; BTW, compiler optimization was turned <em>on</em>.</p>
<table cellspacing="0" cellpadding="2" width="400" border="0">
<tbody>
<tr>
<th width="49%" colspan="2">Case enum <em>in</em> order</th>
<th width="49%" colspan="2">Case enum <em>out of</em> order</th>
</tr>
<tr>
<td colspan="2">
<pre><code></code>  myEnum := my1;
  <code><strong>case</strong></code> myEnum <code><strong>of</strong></code>
    my1: val := <code class="quote">'1'</code>;
    my2: val := <code class="quote">'2'</code>;
    my3: val := <code class="quote">'3'</code>;
    my4: val := <code class="quote">'4'</code>;
  <code><strong>else</strong></code>
    val := <code class="quote">'?'</code>;
  <code><strong>end</strong></code>;</pre>
</td>
<td colspan="2">
<pre><code></code>  myEnum := my1;
  <code><strong>case</strong></code> myEnum <code><strong>of</strong></code>
    my3: val := <code class="quote">'3'</code>;
    my1: val := <code class="quote">'1'</code>;
    my4: val := <code class="quote">'4'</code>;
    my2: val := <code class="quote">'2'</code>;
  <code><strong>else</strong></code>
    val := <code class="quote">'?'</code>;
  <code><strong>end</strong></code>;</pre>
</td>
</tr>
<tr>
<th colspan="4"><em>&#8212;&#8211; Disassembly &#8212;&#8211;</em></th>
</tr>
<tr>
<td colspan="2"><em>case myEnum of</em></td>
<td colspan="2"><em>case myEnum of</em></td>
</tr>
<tr>
<td width="25%">BF9E</td>
<td width="24%">sub al,$01</td>
<td width="24%">C00A</td>
<td width="25%">sub al,$01</td>
</tr>
<tr>
<td>BFA0</td>
<td>jb $bfae</td>
<td>C00C</td>
<td>jb $c01e</td>
</tr>
<tr>
<td>BFA2</td>
<td>jz $bfb2</td>
<td>C00E</td>
<td>jz $c026</td>
</tr>
<tr>
<td>BFA4</td>
<td>dec al</td>
<td>C010</td>
<td>dec al</td>
</tr>
<tr>
<td>BFA6</td>
<td>jz $bfb6</td>
<td>C012</td>
<td>jz $c01a</td>
</tr>
<tr>
<td>BFA8</td>
<td>dec al</td>
<td>C014</td>
<td>dec al</td>
</tr>
<tr>
<td>BFAA</td>
<td>jz $bfba</td>
<td>C016</td>
<td>jz $c022</td>
</tr>
<tr>
<td>BFAC</td>
<td>jmp $bfbe</td>
<td>C018</td>
<td>jmp $c02a</td>
</tr>
<tr>
<td colspan="2"><em>my1: val := &#8216;1&#8242;;</em></td>
<td colspan="2"><em>my3: val := &#8216;3&#8242;;</em></td>
</tr>
<tr>
<td>BFAE</td>
<td>mov bl,$31</td>
<td>C01A</td>
<td>mov bl,$33</td>
</tr>
<tr>
<td>BFB0</td>
<td>jmp $bfc0</td>
<td>C01C</td>
<td>jmp $c02c</td>
</tr>
<tr>
<td colspan="2"><em>my2: val := &#8216;2&#8242;;</em></td>
<td colspan="2"><em>my1: val := &#8216;1&#8242;;</em></td>
</tr>
<tr>
<td>BFB2</td>
<td>mov bl,$32</td>
<td>C01E</td>
<td>mov bl,$31</td>
</tr>
<tr>
<td>BFB4</td>
<td>jmp $bfc0</td>
<td>C020</td>
<td>jmp $c02c</td>
</tr>
<tr>
<td colspan="2"><em>my3: val := &#8216;3&#8242;;</em></td>
<td colspan="2"><em>my4: val := &#8216;4&#8242;;</em></td>
</tr>
<tr>
<td>BFB6</td>
<td>mov bl,$33</td>
<td>C022</td>
<td>mov bl,$34</td>
</tr>
<tr>
<td>BFB8</td>
<td>jmp $bfc0</td>
<td>C024</td>
<td>jmp $c02c</td>
</tr>
<tr>
<td colspan="2"><em>my4: val := &#8216;4&#8242;;</em></td>
<td colspan="2"><em>my2: val := &#8216;2&#8242;;</em></td>
</tr>
<tr>
<td>BFBA</td>
<td>mov bl,$34</td>
<td>C026</td>
<td>mov bl,$32</td>
</tr>
<tr>
<td>BFBC</td>
<td>jmp $bfc0</td>
<td>C028</td>
<td>jmp $c02c</td>
</tr>
<tr>
<td colspan="2"><em>val := &#8216;?&#8217;;</em></td>
<td colspan="2"><em>val := &#8216;?&#8217;;</em></td>
</tr>
<tr>
<td>BFBE</td>
<td>mov bl,$20</td>
<td>C02A</td>
<td>mov bl,$20</td>
</tr>
</tbody>
</table>
<p>Same number of lines of assembly code. There may be some internal CPU optimization, but I don&#8217;t expect there is any. </p>
<p><strong>Conclusion: </strong>Order of the enum in the case statement does not change performance.&#160; Anyone else have any details I missed, or evidence to the contrary?&#160; I guess the next step is to test it in .NET and IL. . . . </p>
]]></content:encoded>
			<wfw:commentRss>http://www.davinciunltd.com/2008/07/order-of-enum-in-case-statement/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

