<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>JIT optimizations on for loop with ar.Length constraint</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx</link><description>A reader wrote in recently and asked me why the non-caching version of the following code was faster than the caching version. This goes against the intuition of any longtime C\C++ developer. The reader found Mark’s blog that confirmed his findings, but</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: JIT optimizations on for loop with ar.Length constraint</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411342</link><pubDate>Sun, 24 Apr 2005 07:32:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411342</guid><dc:creator>Drew Marsh</dc:creator><description>Any reason you don't just suggest foreach() in this kind of scenario? Since, after all, foreach() over an array ends up expanding to the pattern that is recognized by the JIT upon compilation. Obviously if you need the index ya gotta stick with the for(), but that's usually the rarer case.&lt;br&gt;&lt;br&gt;Just curious,&lt;br&gt;Drew</description></item><item><title>re: JIT optimizations on for loop with ar.Length constraint</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411383</link><pubDate>Sun, 24 Apr 2005 11:44:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411383</guid><dc:creator>Henry Boehlert</dc:creator><description>Thanks, it was helpful indeed. But then again, I'm a little unhappy with that.&lt;br&gt;&lt;br&gt;It's a area where developers can only guess what the right thing is. In your example, what I would have considered a safe bet was wrong and the opposite was true.&lt;br&gt;In Ian Hoff's example (on channel 9), a similar call (Array.GetUpperBound) was the cause of a 25% performance hit.&lt;br&gt;&lt;br&gt;Profiling each and every loop is just too much work. There has to be more consistent guidance to let us make safe 80/20 bets.&lt;br&gt;&lt;br&gt;Performance Considerations on MSDN is a very good summary but it is probably outdated and way to short on JIT. &lt;br&gt;Improving Managed Code Performance says:&lt;br&gt;Avoid repetitive field or property access. &lt;br&gt;&lt;br&gt;I'd like to call for enhanced FxCop rules on performance, so we could get an automated grip on the problem without having to second guess every little loop.&lt;br&gt;&lt;br&gt;I understand that the JIT compiler can only perform so much optimization, so wouldn't it be actually better to leave out obscure features if they are impossible to be implemented consistently?&lt;br&gt;&lt;br&gt;Why can't the C# compiler come to the rescue here? You could put const/readonly propagation and all the special knowledge into it.&lt;br&gt;&lt;br&gt;During (release) compilation to IL you have (almost) all the time in the world.</description></item><item><title>re: JIT optimizations on for loop with ar.Length constraint</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411451</link><pubDate>Sun, 24 Apr 2005 17:58:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411451</guid><dc:creator>Lee</dc:creator><description>Has this been changed in Whidbey?  I've made this flawed optimization before specifically after a &amp;quot;measure, measure, measure&amp;quot; step.  It wasn't flawed when I did it, as that loop construct became 39% faster.  I just tried it again, and it continues to be faster.</description></item><item><title>Premature optimization is</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411462</link><pubDate>Sun, 24 Apr 2005 18:14:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411462</guid><dc:creator>The Blog</dc:creator><description /></item><item><title>re: JIT optimizations on for loop with ar.Length constraint</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411602</link><pubDate>Mon, 25 Apr 2005 08:14:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411602</guid><dc:creator>Alex Suzuki</dc:creator><description>Brad: Thanks for the answer. That reader you mentioned was me. ;-)&lt;br&gt;&lt;br&gt;Lee: I can confirm that. The for loop that caches the length is no longer slower in Whidbey. It seems to perform pretty much identically.</description></item><item><title>re: JIT optimizations on for loop with ar.Length constraint</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411734</link><pubDate>Mon, 25 Apr 2005 15:19:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411734</guid><dc:creator>ahnan</dc:creator><description>I don't think that's convenient. Not only for developers but also other languages/compliers. It just confused me.</description></item><item><title>re: JIT optimizations on for loop with ar.Length constraint</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411777</link><pubDate>Mon, 25 Apr 2005 17:45:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411777</guid><dc:creator>Mike Swaim</dc:creator><description>I know about that optimization, but what about ArrayLists? I use them a whole lot more than arrays.</description></item><item><title>Loop Optimizations</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411879</link><pubDate>Mon, 25 Apr 2005 22:24:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411879</guid><dc:creator>Javier G. Lozano</dc:creator><description /></item><item><title>Loop Optimizations</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#411882</link><pubDate>Mon, 25 Apr 2005 22:27:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411882</guid><dc:creator>Javier G. Lozano</dc:creator><description /></item><item><title>Not your expected behaviour</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#414212</link><pubDate>Tue, 03 May 2005 12:52:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:414212</guid><dc:creator>the roarty blog</dc:creator><description>Brad Abrams explains a JIT optimisation which could catch you out.&lt;br&gt;This kinda stuff is interesting,...</description></item><item><title>For Loops and Performance</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#626981</link><pubDate>Mon, 12 Jun 2006 01:32:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:626981</guid><dc:creator>Greg Young [MVP]</dc:creator><description>This article discusses looping performance at the lowest level (native). It dispels a few myths that have been circulating the blogosphere for quite some time and makes some general performance recommendations.</description></item><item><title>An in depth look at for loops</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#626989</link><pubDate>Mon, 12 Jun 2006 02:10:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:626989</guid><dc:creator>Greg Young [MVP]</dc:creator><description>This article discusses looping performance at the lowest level (native). It dispels a few myths that have been circulating the blogosphere and makes some general performance recommendations.</description></item><item><title>Efficient For Loop</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#646621</link><pubDate>Sun, 25 Jun 2006 17:35:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:646621</guid><dc:creator>jokiz's blog</dc:creator><description>[code language=&amp;amp;quot;C#&amp;amp;quot;]for (int i = 0; i &amp;amp;amp;lt; collection.Count; i++){&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;&amp;amp;amp;nbsp; //do something with...</description></item><item><title>Misconception on the efficient for loop</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#646625</link><pubDate>Sun, 25 Jun 2006 17:54:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:646625</guid><dc:creator>jokiz's blog</dc:creator><description>I have posted an entry on&lt;br&gt;my blog on how to have an efficient for loops by storing the length&lt;br&gt;before...</description></item><item><title>Efficient For Loop</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#695362</link><pubDate>Fri, 11 Aug 2006 14:20:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:695362</guid><dc:creator>jokiz's blog</dc:creator><description>[code language=&amp;amp;quot;C#&amp;amp;quot;]for (int i = 0; i collection.Count; i++){ //do something with collection}[/code]I’ve</description></item><item><title>Misconception on the efficient for loop</title><link>http://blogs.msdn.com/brada/archive/2005/04/23/411321.aspx#695396</link><pubDate>Fri, 11 Aug 2006 14:52:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:695396</guid><dc:creator>jokiz's blog</dc:creator><description>I have posted an entry on my blog on how to have an efficient for loops by storing the length before</description></item></channel></rss>