Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The lamda version is actually not slower. I just did a quick test and the delegate version is about 30% faster.</p> <p>Here is the codez:</p> <pre><code>class Blah { public void DoStuff() { } } List&lt;Blah&gt; blahs = new List&lt;Blah&gt;(); DateTime start = DateTime.Now; for(int i = 0; i &lt; 30000000; i++) { blahs.Add(new Blah()); } TimeSpan elapsed = (DateTime.Now - start); Console.WriteLine(string.Format(System.Globalization.CultureInfo.CurrentCulture, "Allocation - {0:00}:{1:00}:{2:00}.{3:000}", elapsed.Hours, elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds)); start = DateTime.Now; foreach(var bl in blahs) { bl.DoStuff(); } elapsed = (DateTime.Now - start); Console.WriteLine(string.Format(System.Globalization.CultureInfo.CurrentCulture, "foreach - {0:00}:{1:00}:{2:00}.{3:000}", elapsed.Hours, elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds)); start = DateTime.Now; blahs.ForEach(bl=&gt;bl.DoStuff()); elapsed = (DateTime.Now - start); Console.WriteLine(string.Format(System.Globalization.CultureInfo.CurrentCulture, "lambda - {0:00}:{1:00}:{2:00}.{3:000}", elapsed.Hours, elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds)); </code></pre> <p>OK, So I've run more tests and here are the results.</p> <ol> <li><p>The order of the execution(forach, lambda or lambda, foreach) didn't make much difference, lambda version was still faster:</p> <pre>foreach - 00:00:00.561 lambda - 00:00:00.389 lambda - 00:00:00.317 foreach - 00:00:00.337</pre></li> <li><p>The difference in performance is a lot less for arrays of classes. Here are the numbers for Blah[30000000]:</p> <pre>lambda - 00:00:00.317 foreach - 00:00:00.337</pre></li> <li><p>Here is the same test but Blah being a struct:</p> <pre>Blah[] version lambda - 00:00:00.676 foreach - 00:00:00.437 List version: lambda - 00:00:00.461 foreach - 00:00:00.391</pre></li> <li><p>Optimized build, Blah is a struct using an array.</p> <pre>lambda - 00:00:00.426 foreach - 00:00:00.079</pre></li> </ol> <p><strong>Conclusion:</strong> There is no blanket answer for performance of foreach vs lambda. The answer is <em>It depends</em>. <a href="http://diditwith.net/2006/10/05/PerformanceOfForeachVsListForEach.aspx" rel="nofollow noreferrer">Here</a> is a more scientific test for <code>List&lt;T&gt;</code>. As far as I can tell it's pretty damn efficient. If you are really concerned with performance use <code>for(int i...</code> loop. For iterating over a collection of a thousand customer records (example) it really doesn't matter all that much.</p> <p>As far as deciding between which version to use I would put potential performance hit for lambda version way at the bottom.</p> <p><strong>Conclusion #2</strong> <code>T[]</code> (where T is a value type) foreach loop is about 5 times faster for this test in an optimized build. That's the only significant difference between a Debug and Release build. So there you go, for arrays of value types use foreach, everything else - it doesn't matter.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload