<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-1328514867467683594.post6050002270667704889..comments</id><updated>2012-01-31T12:21:19.516-08:00</updated><category term='wcf'/><category term='LINQ'/><category term='technology'/><category term='jQuery'/><category term='MySQL'/><category term='SQL'/><category term='TFS'/><category term='bug fixes'/><category term='windows azure'/><category term='unit testing'/><category term='asp.net'/><category term='google charts'/><category term='code snippets'/><category term='general'/><category term='church management'/><category term='IIS'/><category term='asp.net mvc'/><title type='text'>Comments on Schnieds Blog: LINQ vs FOREACH vs FOR Loop Performance</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.schnieds.com/feeds/6050002270667704889/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html'/><author><name>Aaron Schnieder</name><uri>http://www.blogger.com/profile/13203827771563617211</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1328514867467683594.post-635801226176481544</id><published>2010-11-10T08:32:44.095-08:00</published><updated>2010-11-10T08:32:44.095-08:00</updated><title type='text'>Hi, I do some tests and I found two very fast solu...</title><content type='html'>Hi, I do some tests and I found two very fast solutions :&lt;br /&gt;&lt;br /&gt;&lt;b&gt;1) First method :&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;booleanValues = GetBooleanList(numberOfTests);&lt;br /&gt;startTime = DateTime.Now;&lt;br /&gt;&lt;br /&gt;bool[] array = booleanValues.ToArray();&lt;br /&gt;booleanValues = new List();&lt;br /&gt;&lt;br /&gt;for (int i = 0; i &amp;lt; array.Length ; i++)&lt;br /&gt;   if (array[i])&lt;br /&gt;      booleanValues.Add(array[i]);&lt;br /&gt;&lt;br /&gt;Console.WriteLine(string.Format(&amp;quot;Very Fast For Loop results: Bool Count = {0}, Time = {1}&amp;quot;, booleanValues.Count, DateTime.Now.Subtract(startTime)));&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2) Second method :&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;booleanValues = GetBooleanList(numberOfTests);&lt;br /&gt;startTime = DateTime.Now;&lt;br /&gt;&lt;br /&gt;booleanValues.RemoveAll(k =&amp;gt; !k);&lt;br /&gt;&lt;br /&gt;Console.WriteLine(string.Format(&amp;quot;Very Fast LINQ results: Bool Count = {0}, Time = {1}&amp;quot;, booleanValues.Count, DateTime.Now.Subtract(startTime)));&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each of these method returned an incredible time of &lt;b&gt;00:00:00&lt;/b&gt; for 500000 entries in the initial booleanValues, with 250000 == true and 250000 == false.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/635801226176481544'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/635801226176481544'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html?showComment=1289406764095#c635801226176481544' title=''/><author><name>Damien</name><uri>http://www.blogger.com/profile/01262047771132517161</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html' ref='tag:blogger.com,1999:blog-1328514867467683594.post-6050002270667704889' source='http://www.blogger.com/feeds/1328514867467683594/posts/default/6050002270667704889' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1320397554'/></entry><entry><id>tag:blogger.com,1999:blog-1328514867467683594.post-4948872645917518910</id><published>2010-07-29T16:56:20.867-07:00</published><updated>2010-07-29T16:56:20.867-07:00</updated><title type='text'>Just now getting around to looking into linq, came...</title><content type='html'>Just now getting around to looking into linq, came across this when looking at performance vs loops. Nice writeup Aaron, thanks ;)</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/4948872645917518910'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/4948872645917518910'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html?showComment=1280447780867#c4948872645917518910' title=''/><author><name>drokliss</name><uri>http://www.blogger.com/profile/16791447056466870486</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html' ref='tag:blogger.com,1999:blog-1328514867467683594.post-6050002270667704889' source='http://www.blogger.com/feeds/1328514867467683594/posts/default/6050002270667704889' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-2053657270'/></entry><entry><id>tag:blogger.com,1999:blog-1328514867467683594.post-5674805009125983618</id><published>2009-07-01T15:45:39.049-07:00</published><updated>2009-07-01T15:45:39.049-07:00</updated><title type='text'>I am afraid, you just used Linq improperly. Your s...</title><content type='html'>I am afraid, you just used Linq improperly. Your statement generates in fact cartesian product as it does not have an idea that each element is only once in the collection where you delete it from.&lt;br /&gt;it should be:&lt;br /&gt;booleanValues = booleanValues.Where(x=&amp;gt;x).ToList();&lt;br /&gt;i.e. just get elements which do NOT satisfy criteria for delete.&lt;br /&gt;In this scenarion speed is BETTER than for loop.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/5674805009125983618'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/5674805009125983618'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html?showComment=1246488339049#c5674805009125983618' title=''/><author><name>ZByszek</name><uri>http://www.blogger.com/profile/14113850167932370942</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html' ref='tag:blogger.com,1999:blog-1328514867467683594.post-6050002270667704889' source='http://www.blogger.com/feeds/1328514867467683594/posts/default/6050002270667704889' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1343576404'/></entry><entry><id>tag:blogger.com,1999:blog-1328514867467683594.post-4580532051631743696</id><published>2009-03-25T08:44:00.000-07:00</published><updated>2009-03-25T08:44:00.000-07:00</updated><title type='text'>Paulo:&lt;br&gt;That is a great comment and exactly what...</title><content type='html'>Paulo:&lt;BR/&gt;That is a great comment and exactly what I was getting at in my post. LINQ is AWESOME and in my opinion one of the best new releases in the .NET Framework. However, because there are performance considerations all developers that choose to use LINQ should consider the performance implications in their decision.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/4580532051631743696'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/4580532051631743696'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html?showComment=1237995840000#c4580532051631743696' title=''/><author><name>Aaron Schnieder</name><uri>http://www.blogger.com/profile/13203827771563617211</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html' ref='tag:blogger.com,1999:blog-1328514867467683594.post-6050002270667704889' source='http://www.blogger.com/feeds/1328514867467683594/posts/default/6050002270667704889' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1682580389'/></entry><entry><id>tag:blogger.com,1999:blog-1328514867467683594.post-7919623417467282230</id><published>2009-03-25T08:43:00.000-07:00</published><updated>2009-03-25T08:43:00.000-07:00</updated><title type='text'>Don:&lt;br&gt;&lt;br&gt;I updated the post with my system spec...</title><content type='html'>Don:&lt;BR/&gt;&lt;BR/&gt;I updated the post with my system specs. I am currently running .NET Framework 3.5 SP1.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/7919623417467282230'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/7919623417467282230'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html?showComment=1237995780000#c7919623417467282230' title=''/><author><name>Aaron Schnieder</name><uri>http://www.blogger.com/profile/13203827771563617211</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html' ref='tag:blogger.com,1999:blog-1328514867467683594.post-6050002270667704889' source='http://www.blogger.com/feeds/1328514867467683594/posts/default/6050002270667704889' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1682580389'/></entry><entry><id>tag:blogger.com,1999:blog-1328514867467683594.post-3351583097580864211</id><published>2009-03-24T10:57:00.000-07:00</published><updated>2009-03-24T10:57:00.000-07:00</updated><title type='text'>It's true. For now the LINQ is slower then the usu...</title><content type='html'>It's true. For now the LINQ is slower then the usual foreach method. The good thing is, the LINQ is on the framework and that can be improved.&lt;BR/&gt;&lt;BR/&gt;I remember how was the concatenate of string. In the begin we use the + to concatenate but now we use StringBuilder or string.Format because is better and faster and more readable.&lt;BR/&gt;&lt;BR/&gt;I'm sute the LINQ will be better in the future. For now, it's your choice to use it or not.&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;Regards&lt;BR/&gt;Paulo Aboim Pinto&lt;BR/&gt;Odivelas - Portugal</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/3351583097580864211'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/3351583097580864211'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html?showComment=1237917420000#c3351583097580864211' title=''/><author><name>Paulo Aboim Pinto</name><uri>http://www.blogger.com/profile/16646569685451640422</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html' ref='tag:blogger.com,1999:blog-1328514867467683594.post-6050002270667704889' source='http://www.blogger.com/feeds/1328514867467683594/posts/default/6050002270667704889' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-965177356'/></entry><entry><id>tag:blogger.com,1999:blog-1328514867467683594.post-7037944275666112072</id><published>2009-03-24T10:20:00.000-07:00</published><updated>2009-03-24T10:20:00.000-07:00</updated><title type='text'>It&amp;#39;s hard to outperform the for() loop.&lt;br&gt;&lt;br...</title><content type='html'>It&amp;#39;s hard to outperform the for() loop.&lt;BR/&gt;&lt;BR/&gt;BTW, I like to start the for loop at the end of the list and work backwards.  so like: for(i = list.Count - 1; i &amp;lt; list.count; i--)&lt;BR/&gt;&lt;BR/&gt;Then you don&amp;#39;t have to do the i-- if you remove something, as you&amp;#39;re already doing it.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/7037944275666112072'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/7037944275666112072'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html?showComment=1237915200000#c7037944275666112072' title=''/><author><name>Steve Sheldon</name><uri>http://www.blogger.com/profile/06168751796540593645</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html' ref='tag:blogger.com,1999:blog-1328514867467683594.post-6050002270667704889' source='http://www.blogger.com/feeds/1328514867467683594/posts/default/6050002270667704889' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1049937489'/></entry><entry><id>tag:blogger.com,1999:blog-1328514867467683594.post-1624683627249964792</id><published>2009-03-24T10:16:00.000-07:00</published><updated>2009-03-24T10:16:00.000-07:00</updated><title type='text'>Someone else did a similar experiment (I can't fin...</title><content type='html'>Someone else did a similar experiment (I can't find the link) but I did a similar thing and using 3.5 SP1, a simple LINQ select instead of a ForEach is 20% slower than the ForEach.  But, YMMV.  If I remember correctly, there was some changes put in SP1 that increased perf in certain situations.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/1624683627249964792'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1328514867467683594/6050002270667704889/comments/default/1624683627249964792'/><link rel='alternate' type='text/html' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html?showComment=1237914960000#c1624683627249964792' title=''/><author><name>Don Demsak</name><uri>http://www.donxml.com</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img1.blogblog.com/img/blank.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html' ref='tag:blogger.com,1999:blog-1328514867467683594.post-6050002270667704889' source='http://www.blogger.com/feeds/1328514867467683594/posts/default/6050002270667704889' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1929250500'/></entry></feed>
