Bad Linq Performance in Loops

Linq does make many things easier. It also makes many things much more elegant. But sometimes it makes things a lot more complicated, too.
Let’s have a look at this code

// let's generate some items in a simple list
var myEnum = new List<string> { "a", "b", "c", "d", "e" };

// now we order the list
var myOrderedList = from x in myEnum orderby x.CustomOrderBy("d") select x;

To do something with the list, I will do some lookups in a loop:

// lets enumerate the list items
foreach (var listItem in myEnum)
    // for each of the elements filter the list for all items 
    // "less" than the current item
    var myItem = (from x in myOrderedList 
                  where x.CustomWhere(listItem)
                  select x).FirstOrDefault();

As you can see I’ve used two methods (extension methods that will simply count the method calls) for sorting and filtering. So let’s have a look for the method calls:
CustomOrderBy: 25 Aufrufe
CustomWhere: 19 Aufrufe
Whow! 25 calls to CustomOrderBy – with a simple sorting algorithm I would expect 5 … there’s only one statement iterating the elements – but wait: the linq statement does NOT iterate the elements; it’s generating a class that will iterate the elements when accessing it. So each time we access the result of the Linq query, the query is being executed – in our case 5 times in the for-loop = 25 executions for the where statement.
The solution is really simple:

var myOrderedList = (from x in myEnum orderby x.CustomOrderBy("d") select x).ToList();

This will enforce the execution of the linq query one time outside the loop and convert the IEnumeratable into a List. In this case we do have a type containing the elements and not the definition how to get the elements, so the “where” will not be executed again.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Sylvio's Infobox

Aktuelle Themen rund um SQL Server, BI, Windows, ...

Meredith Lewis

Professional Digital Portfolio

Vittorio Bertocci

Just another weblog

ScottGu's Blog

Just another weblog

AJ's blog

Thoughts and informations I think worthwhile to share...

Outlawtrail - .NET Development

Architecture & Design

SDX eXperts Flurfunk

Just another weblog

%d bloggers like this: