<?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: LINQ vs Loop &#8211; A performance test</title>
	<atom:link href="http://ox.no/posts/linq-vs-loop-a-performance-test/feed" rel="self" type="application/rss+xml" />
	<link>http://ox.no/posts/linq-vs-loop-a-performance-test</link>
	<description>Håvard Stranden&#039;s website</description>
	<lastBuildDate>Thu, 29 Mar 2012 00:08:08 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Håvard</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-1353</link>
		<dc:creator>Håvard</dc:creator>
		<pubDate>Thu, 24 Mar 2011 11:33:00 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-1353</guid>
		<description>&lt;p&gt;CmdJerry:&lt;/p&gt;

&lt;p&gt;Your first source at best misleading, at worst wrong. (Theoretically, one might create a case where LINQ queries iterate across enumerables that themselves iterate the entire result set once for each item, but that&#039;s irrelevant to the understanding of LINQ and its behavior.)&lt;/p&gt;

&lt;p&gt;Your second source explains the behavior correctly: LINQ queries use deferred execution, which implies that nothing is executed until you iterate the results of your query. Further, iteration with a plain loop will iterate the enumerable once and only once.&lt;/p&gt;

&lt;p&gt;Calling &lt;code&gt;ToList()&lt;/code&gt; is just the same as writing a loop which adds the items to a list:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;&lt;code&gt;var yourQuery = from item in manyItems select item;
// Calling ToList()...
var listOfItems = yourQuery.ToList();
// ... is equivalent to building your own list in a loop
var list = new List&lt;T&gt;();
foreach(var item in yourQuery)
    list.Add(item);
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;So there is nothing to gain performance-wise by calling &lt;code&gt;ToList()&lt;/code&gt; when iterating across the results once. In fact, calling &lt;code&gt;ToList()&lt;/code&gt; and then looping will perform worse than just looping. There&#039;s nothing magic to it.&lt;/p&gt;

&lt;p&gt;I recommend you read up on deferred execution and the &lt;code&gt;yield&lt;/code&gt; statement. &lt;a href=&quot;http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx&quot; rel=&quot;nofollow&quot;&gt;Charlie Calvert&#039;s post on LINQ and deferred execution&lt;/a&gt; is a good start.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>CmdJerry:</p>

<p>Your first source at best misleading, at worst wrong. (Theoretically, one might create a case where LINQ queries iterate across enumerables that themselves iterate the entire result set once for each item, but that&#8217;s irrelevant to the understanding of LINQ and its behavior.)</p>

<p>Your second source explains the behavior correctly: LINQ queries use deferred execution, which implies that nothing is executed until you iterate the results of your query. Further, iteration with a plain loop will iterate the enumerable once and only once.</p>

<p>Calling <code>ToList()</code> is just the same as writing a loop which adds the items to a list:</p>

<p><pre><code>var yourQuery = from item in manyItems select item;
// Calling ToList()...
var listOfItems = yourQuery.ToList();
// ... is equivalent to building your own list in a loop
var list = new List&lt;T&gt;();
foreach(var item in yourQuery)
    list.Add(item);
</code></pre></p>

<p>So there is nothing to gain performance-wise by calling <code>ToList()</code> when iterating across the results once. In fact, calling <code>ToList()</code> and then looping will perform worse than just looping. There&#8217;s nothing magic to it.</p>

<p>I recommend you read up on deferred execution and the <code>yield</code> statement. <a href="http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx" rel="nofollow">Charlie Calvert&#8217;s post on LINQ and deferred execution</a> is a good start.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: CmdJerry</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-1351</link>
		<dc:creator>CmdJerry</dc:creator>
		<pubDate>Thu, 24 Mar 2011 03:46:38 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-1351</guid>
		<description>&lt;p&gt;The LINQ query object still is used underneath the enumerable object when looping. This means the query is called repeatedly when looping. Take a look at these two articles &lt;a href=&quot;http://allthingscs.blogspot.com/2011/03/linq-performance.html&quot; rel=&quot;nofollow&quot;&gt;http://allthingscs.blogspot.com/2011/03/linq-performance.html&lt;/a&gt; and &lt;a href=&quot;http://odetocode.com/code/737.aspx&quot; rel=&quot;nofollow&quot;&gt;http://odetocode.com/code/737.aspx&lt;/a&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The LINQ query object still is used underneath the enumerable object when looping. This means the query is called repeatedly when looping. Take a look at these two articles <a href="http://allthingscs.blogspot.com/2011/03/linq-performance.html" rel="nofollow">http://allthingscs.blogspot.com/2011/03/linq-performance.html</a> and <a href="http://odetocode.com/code/737.aspx" rel="nofollow">http://odetocode.com/code/737.aspx</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: LinqTrialerandHater</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-1347</link>
		<dc:creator>LinqTrialerandHater</dc:creator>
		<pubDate>Fri, 18 Mar 2011 05:14:36 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-1347</guid>
		<description>&lt;p&gt;Bah Linq, ever thought of what your going to do if you wish to port your application to a different language that doesn&#039;t use Lamda expressions or Linq! youre stuffed! there really is no difference between linq and a for loop. If it was faster than a for loop I would think its cool, but its not and it&#039;s also harder to look at, understand and debug. It&#039;s just a bunch a snobs trying to be smarter and cleverer than everyone else, Wow! look at the weird and cryptic linq expression I can write! it&#039;s slower and harder to understand and if I ever want to port my code to objective C++ or PHP or something. I can just sit on it, and go wow, I thought it was cool but its slower and really just microsofts way of keeping you locked into their product! So THERE!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Bah Linq, ever thought of what your going to do if you wish to port your application to a different language that doesn&#8217;t use Lamda expressions or Linq! youre stuffed! there really is no difference between linq and a for loop. If it was faster than a for loop I would think its cool, but its not and it&#8217;s also harder to look at, understand and debug. It&#8217;s just a bunch a snobs trying to be smarter and cleverer than everyone else, Wow! look at the weird and cryptic linq expression I can write! it&#8217;s slower and harder to understand and if I ever want to port my code to objective C++ or PHP or something. I can just sit on it, and go wow, I thought it was cool but its slower and really just microsofts way of keeping you locked into their product! So THERE!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Sanjay</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-1308</link>
		<dc:creator>Sanjay</dc:creator>
		<pubDate>Fri, 04 Mar 2011 16:09:01 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-1308</guid>
		<description>&lt;p&gt;My test indicate that it a lot depends on how &quot;random&quot; the data in the ints array is. Adding the following to shuffle data to be &lt; 10 and &gt; 10
 for (int j = 0; j &lt; SIZE; j++)
            {&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;&lt;code&gt;            if(j%2 == 0)
                ints[j] = 5;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;makes LINQ faster than the loop. Then again, I am testing in 2011. So a lot would have changed since this initial post in 2007.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>My test indicate that it a lot depends on how &#8220;random&#8221; the data in the ints array is. Adding the following to shuffle data to be &lt; 10 and &gt; 10
 for (int j = 0; j &lt; SIZE; j++)
            {</p>

<p><pre><code>            if(j%2 == 0)
                ints[j] = 5;</code></pre></p>

<pre><code>    }
</code></pre>

<p></p>

<p>makes LINQ faster than the loop. Then again, I am testing in 2011. So a lot would have changed since this initial post in 2007.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Performance Tip: Avoid using Linq Loops &#171; rjchicago</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-880</link>
		<dc:creator>Performance Tip: Avoid using Linq Loops &#171; rjchicago</dc:creator>
		<pubDate>Thu, 30 Sep 2010 21:12:01 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-880</guid>
		<description>&lt;p&gt;[...] http://ox.no/posts/linq-vs-loop-a-performance-test [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[...] <a href="http://ox.no/posts/linq-vs-loop-a-performance-test" rel="nofollow">http://ox.no/posts/linq-vs-loop-a-performance-test</a> [...]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: funky</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-688</link>
		<dc:creator>funky</dc:creator>
		<pubDate>Thu, 15 Apr 2010 14:36:25 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-688</guid>
		<description>&lt;p&gt;Thanks! I was getting 50% of CPU usage by writing file to a Stream, like so:&lt;/p&gt;

&lt;p&gt;buffer = formData.Skip(offset).Take(buffer.Length).ToArray();&lt;/p&gt;

&lt;p&gt;After reading your comments, I&#039;ve rewritten my code to use loop and now CPU is quiet as ever :)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks! I was getting 50% of CPU usage by writing file to a Stream, like so:</p>

<p>buffer = formData.Skip(offset).Take(buffer.Length).ToArray();</p>

<p>After reading your comments, I&#8217;ve rewritten my code to use loop and now CPU is quiet as ever <img src='http://ox.no/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
	</item>
	<item>
		<title>By: Ray</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-601</link>
		<dc:creator>Ray</dc:creator>
		<pubDate>Sat, 12 Sep 2009 04:03:35 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-601</guid>
		<description>&lt;p&gt;My code for Linq:&lt;/p&gt;

&lt;p&gt;for (int t = 0; t &lt; RUNS; ++t)
 {
        int[] less = ints.Where(i =&gt; i &lt; 10).ToArray();
 }&lt;/p&gt;

&lt;p&gt;My result:
LINQ: 00:00:00.0936002, avg. 00:00:00.0000936
Loop: 00:00:00.1092002, avg. 00:00:00.0001092&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>My code for Linq:</p>

<p>for (int t = 0; t &lt; RUNS; ++t)
 {
        int[] less = ints.Where(i =&gt; i &lt; 10).ToArray();
 }</p>

<p>My result:
LINQ: 00:00:00.0936002, avg. 00:00:00.0000936
Loop: 00:00:00.1092002, avg. 00:00:00.0001092</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Kaufman</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-531</link>
		<dc:creator>Eric Kaufman</dc:creator>
		<pubDate>Fri, 15 May 2009 19:38:48 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-531</guid>
		<description>&lt;p&gt;Just wanted to add in that with .Net 4.0 they&#039;re upgrading the compiler, and adding parallelism for LINQ queries, which I expect will change things out a lot. For those of you who did hop on the LINQ train, I would imagine that a lot of your queries might just start to work a bit quicker once the new compiler comes out.&lt;/p&gt;

&lt;p&gt;http://www.betanews.com/article/Everyone-talk-at-once-NET-40-will-include-Parallel-Extensions/1223931673&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Just wanted to add in that with .Net 4.0 they&#8217;re upgrading the compiler, and adding parallelism for LINQ queries, which I expect will change things out a lot. For those of you who did hop on the LINQ train, I would imagine that a lot of your queries might just start to work a bit quicker once the new compiler comes out.</p>

<p><a href="http://www.betanews.com/article/Everyone-talk-at-once-NET-40-will-include-Parallel-Extensions/1223931673" rel="nofollow">http://www.betanews.com/article/Everyone-talk-at-once-NET-40-will-include-Parallel-Extensions/1223931673</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-529</link>
		<dc:creator>Brian</dc:creator>
		<pubDate>Sat, 09 May 2009 18:29:11 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-529</guid>
		<description>&lt;p&gt;This function does nothing, and thus benchmarking a compiled version is dangerous (so is benchmarking a debug version, but less so).  A compiler will happily say, &quot;This loop does nothing! Let&#039;s skip it!&quot; I&#039;d be hesitant to trust any benchmarks that are not in use on production code...though they&#039;re a nice tool for developers of compilers to use to check if their optimizations are working.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This function does nothing, and thus benchmarking a compiled version is dangerous (so is benchmarking a debug version, but less so).  A compiler will happily say, &#8220;This loop does nothing! Let&#8217;s skip it!&#8221; I&#8217;d be hesitant to trust any benchmarks that are not in use on production code&#8230;though they&#8217;re a nice tool for developers of compilers to use to check if their optimizations are working.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: JS</title>
		<link>http://ox.no/posts/linq-vs-loop-a-performance-test/comment-page-1#comment-509</link>
		<dc:creator>JS</dc:creator>
		<pubDate>Wed, 25 Feb 2009 21:08:27 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/linq-vs-loop-a-performance-test#comment-509</guid>
		<description>&lt;p&gt;If you run the compiled verision of the application (.exe or .dll), LINQ actually runs faster&lt;/p&gt;

&lt;p&gt;running out of VS is not a valid test and should not be compared.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>If you run the compiled verision of the application (.exe or .dll), LINQ actually runs faster</p>

<p>running out of VS is not a valid test and should not be compared.</p>]]></content:encoded>
	</item>
</channel>
</rss>

