LINQ Performance - In Memory Collection
Steve Herbert has an interesting performance evaluation discussion up on his blog (this is raw LINQ, by the way, not LINQ to SQL). Initially, he has 3 samples, the first 2 using LINQ and the 3rd without. These are the results:
Item#1 – averaged 2643 ms
Item#2 – averaged 671 ms
Item#3 – averaged <1 ms
However, make sure to read through the comments on the post. What emerges is from Eric Lee who comments:
"So the lesson to learn here is that when you use Linq, you either need to make sure your container implements IQueryable or be prepared for IEnumerable-based algorithm performance ... How could Linq possibly know how to take advantage of the quirks of every conceivable data structure? Obviously it can't. It has its own strategy that works against anything that can manage to implement IEnumerable, and it can take advantage of custom strategies if they're provided via IQueryable."
The list of structures and frameworks that implement IQueryable include LINQ To SQL, LINQ To Datasets, LINQ To XML, LINQ To Amazon, LINQ To SharePoint, LINQ To Active Directory, LINQ To Flickr, LINQ To NHibernate, LINQ To REST, even an attempt at LINQ To Google Desktop and I'm sure there are more coming.
For a discussion on LINQ To SQL performance, see my post on this.