Linq to Objects vs Generic List

If you know me, then you know I like to make things run fast.  I like testing performance of my software.  So with a recent website, I noticed that I query a tiny database table a lot.  There surely is a faster why to do it, and with Linq statements you can query in memory objects like Sql.  Nice…?

I was reading this post http://peterkellner.net/2009/06/03/linq-in-memory-performance/ and noticed the author was talking about almost the same thing as I was doing, so read his post, and it will tell you almost what I was thinking.

However I have a problem with his code.  I know he is just making a point, that using plain old c# will always be faster, but I think what he is saying if I read between the lines properly is that why not take the small hit in performance for nicer looking code?

Well if you read his post and agree that 0.52 seconds to run 100,000 statements like his via Linq is fine, then in my opinion you need to read about the methods with List<T>.  If you take his:

var ids = myList.Where(a => a > 5).ToList();

code and turn it into:

var ids = myList.FindAll(a => a > 5);

then you will see the almost the same performance as the plain old c#.  So why even consider Linq when you have a Generic list object.  I know his example is simple, but I wanted to make sure that if someone reads his post, then they need to be careful about “just using Linq cause it’s almost as fast” when there are other options that are even better – using FindAll was only 7 milliseconds slower!

Another slightly unrelated rant is MVP…Microsoft’s Most Valuable Professional.  I think the wording is wrong, as MVP is singular.  It should be MVP of the month, week, day, etc.  But there are a plethora of MVPs so well they can’t all be the “most valuable” surely?

Rant over.

4 thoughts on “Linq to Objects vs Generic List”

  1. Guilty as charged! I do like LINQ an awful lot and probably go out of my way to use it when it is not always necessary. The FindAll makes more sense, but I don’t quite understand the syntax.

  2. Hey Peter,

    I would have thought the FindAll, and in general the generic List methods, would be second nature. I love how easy it is. But of you read my post on interfaces a couple of posts ago, I found out even they are quite inefficient compared to iList and ReadOnly versions…unless you need a List.

    Am still learning all about performance as my new home grown CMS needs the power…more on that later.

  3. Not sure why, but I never took a liking to findall and the predicate thing. That, and regular expressions seem to hurt my head in a funny way.

    BTW, on the MVP rant, I also wear a biking shirt that says “Tripple Crown Winner”, which means that I’ve completed 3 or more 200 mile single bike rides in one year. Technically, it should be Tripple Crown Finisher, but that just does not sound as good.

  4. I still don’t understand regex. Am glad my mate william and my boss do…

    Congrats on the bike ride…that’s massively impressive! Am lucky of I do 200 miles in a life time.

Leave a Reply

Your email address will not be published.