<?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>To foreach or not to foreach that is the question.</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx</link><description>Recently email was forwarded to me with a link to a page with some performance tips for developers. The second performance tip on the page was: foreach foreach through an array is incredibly slow compared to for (int i = 0; i &amp;lt; array.Length; i) This</description><dc:language>en-GB</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: To foreach or not to foreach that is the question.</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#116079</link><pubDate>Mon, 19 Apr 2004 17:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:116079</guid><dc:creator>Paul Wilson</dc:creator><description>I'm glad to hear that's true for arrays, but it certainly is NOT true for all collections.  I found that foreach loops over HashTables and ArrayLists were rather &amp;quot;slow&amp;quot; compared to simple for loops, which made a big difference.</description></item><item><title>re: To foreach or not to foreach that is the question.</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#116092</link><pubDate>Mon, 19 Apr 2004 17:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:116092</guid><dc:creator>Kevin Ransom</dc:creator><description>Indeed, I hope to spend some time talking about that in a future entry.</description></item><item><title>re: To foreach or not to foreach that is the question.</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#116112</link><pubDate>Mon, 19 Apr 2004 18:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:116112</guid><dc:creator>Jeff Gonzalez</dc:creator><description>That is a huge blanket statement.  If you are using an ArrayList, with value types you would have huge perf consequences due to boxing.   &lt;br&gt;&lt;br&gt;I have heard a similar statement (foreach is slower than for ())  I guess it just depends on the context.</description></item><item><title>re: To foreach or not to foreach that is the question.</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#116167</link><pubDate>Mon, 19 Apr 2004 19:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:116167</guid><dc:creator>Scott Galloway</dc:creator><description>Yup, I posted on Eric Gunnersons blog about what Paul mentions, and I had a long running 'debate' about this on the ASP.NET perf forums. The most useful tool I've found for doing these comparisons (without digging through the IL) is Reflector - the C# output is pretty revealing in these debates.</description></item><item><title>re: To foreach or not to foreach that is the question.</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#116286</link><pubDate>Mon, 19 Apr 2004 22:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:116286</guid><dc:creator>matthew</dc:creator><description>you are right. Foreach is not necessarily better or worse from a performance point of view.&lt;br&gt;&lt;br&gt;But THE most important thing, is that unless you have a specific need to optimize, foreach is just more elegant. If you're only iterating through a few items, who cares either way?&lt;br&gt;&lt;br&gt;Use foreach unless you have a very good reason not to.&lt;br&gt;</description></item><item><title>re: To foreach or not to foreach that is the question.</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#117299</link><pubDate>Wed, 21 Apr 2004 05:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117299</guid><dc:creator>Joshua Allen</dc:creator><description>These are not identical.  Get rid of the int i = a[j]; in the second one and ildasm and you save two instructions inside the loop.  You can save yet another two instructions by avoiding checking &amp;lt; a.Length (set int len = a.Length at the start, and compare len).  I just ildasm'd all three examples myself.  The foreach is the worst of all.  And this example is the most efficient possible foreach loop, since the compiler optimizes for it and normal cases would require IEnumerable.  I personally thing that the while &amp;lt; a.Length is the *worst* option of the three in the *general* case, because you have no freekin' clue what is happening in that property access.  FX guidelines say to not do heay work in a getter, but I have seen plenty of violations.&lt;br&gt;</description></item><item><title>re: To foreach or not to foreach that is the question.</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#117644</link><pubDate>Wed, 21 Apr 2004 18:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117644</guid><dc:creator>Jason Coyne</dc:creator><description>Actually, you do not want to set the length into a temporary variable.  In a for loop, where the length is set as the end condition of the loop, the IL does some optimizations to stop checking for out of bounds errors, because by defenition you are not out of bounds.  &lt;br&gt;&lt;br&gt;When you put the length into a temp variable, it must do those checks on each iteration.  Brad A. has done an extensive analysis of this issue.</description></item><item><title>Loopy Decisions</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#118363</link><pubDate>Thu, 22 Apr 2004 21:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:118363</guid><dc:creator>The Farm: The Tucows Developers' Hangout</dc:creator><description>&amp;lt;p&amp;gt;In his blog, &amp;lt;span style=&amp;quot;font-style: italic;&amp;quot;&amp;gt;Better Living Through Software&amp;lt;/span&amp;gt;, Joshua Allen looks at three different ways to loop in C# from an optimizing-for-performance point of view:&amp;lt;/p&amp;gt;&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;foreach (int i in foo) {}&amp;lt;br&amp;gt;&lt;br&gt;&amp;lt;br&amp;gt;&lt;br&gt;for (int i = 0; i &amp;amp;lt; foo.Length; i++) {}&amp;lt;br&amp;gt;&lt;br&gt;&amp;lt;br&amp;gt;&lt;br&gt;int len = foo.Length; for (int i = 0; ...</description></item><item><title>Eric Gunnerson on Loops</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#131997</link><pubDate>Fri, 14 May 2004 19:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:131997</guid><dc:creator>Bryant Likes's Blog</dc:creator><description /></item><item><title>I think I got it now</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#248077</link><pubDate>Tue, 26 Oct 2004 23:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:248077</guid><dc:creator>Omer van Kloeten's .NET Zen</dc:creator><description /></item><item><title>I think I got it now</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#249550</link><pubDate>Fri, 29 Oct 2004 16:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:249550</guid><dc:creator>dotRob</dc:creator><description /></item><item><title>re: foreach Row in ...</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#270360</link><pubDate>Thu, 25 Nov 2004 19:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:270360</guid><dc:creator>Andrea Piccoli's blog</dc:creator><description /></item><item><title>'for' vs 'foreach'</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#426003</link><pubDate>Tue, 07 Jun 2005 11:10:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:426003</guid><dc:creator>- TrIpLeZoNe -</dc:creator><description>Here are some resources to read up on the differences between 'for' and 'foreach'.&lt;br&gt;Enumerating array...</description></item><item><title>Better Living through Software  &amp;raquo; Blog Archive   &amp;raquo; Loopy Decisions</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#694419</link><pubDate>Thu, 10 Aug 2006 18:14:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:694419</guid><dc:creator>Better Living through Software  » Blog Archive   » Loopy Decisions</dc:creator><description>PingBack from &lt;a rel="nofollow" target="_new" href="http://www.netcrucible.com/blog/2004/04/21/loopy-decisions/"&gt;http://www.netcrucible.com/blog/2004/04/21/loopy-decisions/&lt;/a&gt;</description></item><item><title>Better Living through Software  &amp;raquo; Blog Archive   &amp;raquo; Loopy Decisions</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#694422</link><pubDate>Thu, 10 Aug 2006 18:14:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:694422</guid><dc:creator>Better Living through Software  » Blog Archive   » Loopy Decisions</dc:creator><description>PingBack from &lt;a rel="nofollow" target="_new" href="http://www.netcrucible.com/blog/2004/04/21/loopy-decisions-2/"&gt;http://www.netcrucible.com/blog/2004/04/21/loopy-decisions-2/&lt;/a&gt;</description></item><item><title>for VS. foreach 那个性能更高，为什么，怎么选择</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#4082930</link><pubDate>Fri, 27 Jul 2007 19:30:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4082930</guid><dc:creator>holyrong</dc:creator><description>&lt;p&gt;for和foreach 的效率问题是个老问题了，从网上看到的是众说纷纭，有说for效率高的也有说foreach效率高的，还有说测试方法有问题的；通过实验发现for的效率比foreach高。&lt;/p&gt;
</description></item><item><title>re: [原创]如何改善Managed Code的Performance和Scalability系列之一：由for V.S. for each想到的</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#4280478</link><pubDate>Tue, 07 Aug 2007 20:22:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4280478</guid><dc:creator>cnblogs.com</dc:creator><description>&lt;p&gt;事实上对Array进行foreach的时候根本不会调用GetEnumerator()方法, 也就是被编译器给内联了. 而对于List&amp;amp;lt;&amp;amp;gt;, 虽然没有编译器内联GetEnumerator()&lt;/p&gt;
</description></item><item><title>C#: The Dangers of Foreach</title><link>http://blogs.msdn.com/kevin_ransom/archive/2004/04/19/116072.aspx#8710370</link><pubDate>Wed, 09 Jul 2008 02:56:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8710370</guid><dc:creator>Technical Musings</dc:creator><description>&lt;p&gt;Although very handy, C#&amp;amp;#39;s foreach statement is actually quite dangerous. In fact, I may swear off its use entirely. Why? Two reasons: (1) performance, and (2) predictability....&lt;/p&gt;
</description></item></channel></rss>