<?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: Fast and slow if-statements: branch prediction in modern processors</title>
	<atom:link href="http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/feed/" rel="self" type="application/rss+xml" />
	<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/</link>
	<description>On programming, technology, and random things of interest</description>
	<lastBuildDate>Fri, 11 May 2012 19:47:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	<item>
		<title>By: Igor Ostrovsky</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-12636</link>
		<dc:creator>Igor Ostrovsky</dc:creator>
		<pubDate>Sat, 13 Aug 2011 20:15:25 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-12636</guid>
		<description>tyftyftyf: A more robust way to reproduce the branch prediction effect is to fill a bool array of say length 128 with either a predictable pattern (1010...) or a random pattern of bits. That example should be more hardware-agnostic.</description>
		<content:encoded><![CDATA[<p>tyftyftyf: A more robust way to reproduce the branch prediction effect is to fill a bool array of say length 128 with either a predictable pattern (1010&#8230;) or a random pattern of bits. That example should be more hardware-agnostic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tyftyftyf</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-12613</link>
		<dc:creator>tyftyftyf</dc:creator>
		<pubDate>Sat, 13 Aug 2011 09:34:29 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-12613</guid>
		<description>Everything looks pretty much the same on an i7.
(i &amp; 0x80000000) == 0) =&gt; 3634 msec
(i &amp; 0xffffffff) == 0) =&gt; 4119 msec
(i &amp; 0x80000001) == 0) =&gt; 4149 msec
(i &amp; 0x80000002) == 0) =&gt; 4228 msec
(i &amp; 0x80000003) == 0) =&gt; 4197 msec
(i &amp; 0x80000004) == 0) =&gt; 4180 msec
(i &amp; 0x80000008) == 0) =&gt; 4415 msec
(i &amp; 0x80000010) == 0) =&gt; 4056 msec

Altered the if-statements a bit for fair competition..</description>
		<content:encoded><![CDATA[<p>Everything looks pretty much the same on an i7.<br />
(i &amp; 0&#215;80000000) == 0) =&gt; 3634 msec<br />
(i &amp; 0xffffffff) == 0) =&gt; 4119 msec<br />
(i &amp; 0&#215;80000001) == 0) =&gt; 4149 msec<br />
(i &amp; 0&#215;80000002) == 0) =&gt; 4228 msec<br />
(i &amp; 0&#215;80000003) == 0) =&gt; 4197 msec<br />
(i &amp; 0&#215;80000004) == 0) =&gt; 4180 msec<br />
(i &amp; 0&#215;80000008) == 0) =&gt; 4415 msec<br />
(i &amp; 0&#215;80000010) == 0) =&gt; 4056 msec</p>
<p>Altered the if-statements a bit for fair competition..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ghezal</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-11270</link>
		<dc:creator>Ghezal</dc:creator>
		<pubDate>Mon, 01 Aug 2011 10:15:21 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-11270</guid>
		<description>Can you please given an example to show in which part it take branches and in which part not taken, and also show in example the static and dynamic prediction. Thanks</description>
		<content:encoded><![CDATA[<p>Can you please given an example to show in which part it take branches and in which part not taken, and also show in example the static and dynamic prediction. Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: michal</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-7838</link>
		<dc:creator>michal</dc:creator>
		<pubDate>Sun, 26 Jun 2011 20:28:27 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-7838</guid>
		<description>Hello, very good article!

I did my own tests in C# .NET 4.0 Core i5 3.2 Ghz

const long count = 100000000;
            
            var operands = new uint[] 
            {0x80000000,0xffffffff,1, 2, 3, 4, 8, 6,5,7,9,10,11,111};
            foreach (var operand in operands)
            {                
                var start = DateTime.Now;
                for (int i = 0; i &lt; count; i++)
                {
                    if ((i &amp; operand) == 0) ;
                }
                var milliseconds = (DateTime.Now -start).Milliseconds;
                Console.WriteLine(&quot;{0}: \t{1} ms&quot;, operand, milliseconds);                
            } 

All results was between 400~600ms. So I thing it would be JIT which optimize performance for difficult patterns. There are not so big variances for particular patterns.</description>
		<content:encoded><![CDATA[<p>Hello, very good article!</p>
<p>I did my own tests in C# .NET 4.0 Core i5 3.2 Ghz</p>
<p>const long count = 100000000;</p>
<p>            var operands = new uint[]<br />
            {0&#215;80000000,0xffffffff,1, 2, 3, 4, 8, 6,5,7,9,10,11,111};<br />
            foreach (var operand in operands)<br />
            {<br />
                var start = DateTime.Now;<br />
                for (int i = 0; i &lt; count; i++)<br />
                {<br />
                    if ((i &amp; operand) == 0) ;<br />
                }<br />
                var milliseconds = (DateTime.Now -start).Milliseconds;<br />
                Console.WriteLine(&quot;{0}: \t{1} ms&quot;, operand, milliseconds);<br />
            } </p>
<p>All results was between 400~600ms. So I thing it would be JIT which optimize performance for difficult patterns. There are not so big variances for particular patterns.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harri</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-1106</link>
		<dc:creator>Harri</dc:creator>
		<pubDate>Fri, 03 Sep 2010 10:36:26 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-1106</guid>
		<description>Worrying about branch prediction is most profitable in tight, heavily executed loops. Yet, one can &quot;optimize&quot; in some other cases by recalling that in the absence of branch prediction state (e.g. the first time a particular branch is evaluated) Pentiums predict backwards branches as taken (the common case of loops) but forwards branches as not taken. Hence, it may be beneficial to put the common case as the &quot;true&quot; part of a conditional branch, to increase prediction accuracy. For example:

if (rare boundary cases does not hold)
   do common stuff
else
   handle it

Instead of 

if (rare boundary case)
   handle it
else
   do common stuff

W.r.t predication is useful in some cases but creates data dependencies that may stall the pipeline, as a predicated instruction waits for its condition&#039;s evaluation to complete. A successfully predicted branch breaks such &quot;dependency chains&quot;. Predication works best when it replaces hard-to-predict branches.</description>
		<content:encoded><![CDATA[<p>Worrying about branch prediction is most profitable in tight, heavily executed loops. Yet, one can &#8220;optimize&#8221; in some other cases by recalling that in the absence of branch prediction state (e.g. the first time a particular branch is evaluated) Pentiums predict backwards branches as taken (the common case of loops) but forwards branches as not taken. Hence, it may be beneficial to put the common case as the &#8220;true&#8221; part of a conditional branch, to increase prediction accuracy. For example:</p>
<p>if (rare boundary cases does not hold)<br />
   do common stuff<br />
else<br />
   handle it</p>
<p>Instead of </p>
<p>if (rare boundary case)<br />
   handle it<br />
else<br />
   do common stuff</p>
<p>W.r.t predication is useful in some cases but creates data dependencies that may stall the pipeline, as a predicated instruction waits for its condition&#8217;s evaluation to complete. A successfully predicted branch breaks such &#8220;dependency chains&#8221;. Predication works best when it replaces hard-to-predict branches.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Microprocessor Architecture &#124; Articles About Laptop &#38; Notebooks Online</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-1011</link>
		<dc:creator>Microprocessor Architecture &#124; Articles About Laptop &#38; Notebooks Online</dc:creator>
		<pubDate>Mon, 28 Jun 2010 23:10:58 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-1011</guid>
		<description>[...] Fast and slow if-statements: branch prediction in modern processors [...]</description>
		<content:encoded><![CDATA[<p>[...] Fast and slow if-statements: branch prediction in modern processors [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-1010</link>
		<dc:creator>Henrik</dc:creator>
		<pubDate>Mon, 28 Jun 2010 21:17:08 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-1010</guid>
		<description>You should be aware that &lt;i&gt;A Quantitative Approach&lt;/i&gt; is hardcore (but very, very good). If you are new to this stuff, you might want to consider http://www.amazon.co.uk/Computer-Organization-Design-Fourth-Architecture/dp/0123744938/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1277759360&amp;sr=1-1 from the same authors or perhaps http://www.amazon.co.uk/Inside-Machine-Introduction-Microprocessors-Architecture/dp/1593271042/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1277758880&amp;sr=1-1.</description>
		<content:encoded><![CDATA[<p>You should be aware that <i>A Quantitative Approach</i> is hardcore (but very, very good). If you are new to this stuff, you might want to consider <a href="http://www.amazon.co.uk/Computer-Organization-Design-Fourth-Architecture/dp/0123744938/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1277759360&#038;sr=1-1" rel="nofollow">http://www.amazon.co.uk/Comput.....038;sr=1-1</a> from the same authors or perhaps <a href="http://www.amazon.co.uk/Inside-Machine-Introduction-Microprocessors-Architecture/dp/1593271042/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1277758880&#038;sr=1-1" rel="nofollow">http://www.amazon.co.uk/Inside.....038;sr=1-1</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zun</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-991</link>
		<dc:creator>Zun</dc:creator>
		<pubDate>Wed, 09 Jun 2010 00:53:21 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-991</guid>
		<description>Thank you so much. I will get one for me :)</description>
		<content:encoded><![CDATA[<p>Thank you so much. I will get one for me <img src='http://igoro.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Igor Ostrovsky</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-989</link>
		<dc:creator>Igor Ostrovsky</dc:creator>
		<pubDate>Mon, 07 Jun 2010 17:38:44 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-989</guid>
		<description>Zun, check out this book: http://www.amazon.com/Computer-Architecture-Quantitative-Approach-4th/dp/0123704901/

From what people say, this is the best book on the detais of CPU and memory. I didn&#039;t get to it myself yet, but that&#039;s the book to look at if you want to read more on this topic.</description>
		<content:encoded><![CDATA[<p>Zun, check out this book: <a href="http://www.amazon.com/Computer-Architecture-Quantitative-Approach-4th/dp/0123704901/" rel="nofollow">http://www.amazon.com/Computer.....123704901/</a></p>
<p>From what people say, this is the best book on the detais of CPU and memory. I didn&#8217;t get to it myself yet, but that&#8217;s the book to look at if you want to read more on this topic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zun</title>
		<link>http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/comment-page-1/#comment-987</link>
		<dc:creator>Zun</dc:creator>
		<pubDate>Mon, 07 Jun 2010 14:06:55 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=579#comment-987</guid>
		<description>Could you recommend for me some books about this topic? I really want to read and know more about it. Thank you so much !</description>
		<content:encoded><![CDATA[<p>Could you recommend for me some books about this topic? I really want to read and know more about it. Thank you so much !</p>
]]></content:encoded>
	</item>
</channel>
</rss>

