#Google Analytic Tracker

Pages

Aug 4, 2009

Did you increase or decrease your application performance using IEnumerable in Linq (Part 1)?

I love Linq, there is no doubt about that. I pretty much use Linq whenever is appropriate. Linq simply makes my code much more readable and easier to understand. Another reason is the power the extension methods the Linq library provide for IEnumerable<T> object.

In an older post, I mentioned a bit about IEnumerable and how it works. This time, I am going to talk a bit more about how you can use IEnumerable to help improve your application.

Good Performance using IEnumerable?

Consider the following code:

private bool IsToyotaTheHighestSale2(List<Vehicle> totalSaleList)
{
    int toyotaSale =  (from v in totalSaleList
          where v.Make == Make.Toyota
          select v).Count();

    int hondaSale = (from v in totalSaleList
          where v.Make == Make.Honda
          select v).Count();

    int bmwSale = (from v in totalSaleList
          where v.Make == Make.BMW
          select v).Count();

    //Compare the result
    if (toyotaSale > hondaSale)
        if (toyotaSale > bmwSale)
            return true;

    return false;
}

In the above example, we use Linq to get a count of all the car sales, follow by compare toyta sale with honda and bmw sale.

Now, let check the following:

private bool IsToyotaTheHighestSale2(List<Vehicle> totalSaleList)
{

    IEnumerable<Vehicle> toyota 
        = from v in totalSaleList
          where v.Make == Make.Toyota
          select v;

    IEnumerable<Vehicle> honda 
        = from v in totalSaleList
          where v.Make == Make.Honda
          select v;

    IEnumerable<Vehicle> bmw 
        = from v in totalSaleList
          where v.Make == Make.BMW
          select v;

    //Count all the list first
    int toyotaSale = toyota.Count();

    //Compare the result
    if (toyotaSale > honda.Count())
        if (toyotaSale > bmw.Count())
            return true;

    return false;
}

Comparing IsToyotaTheHighestSale() with IsToyotaTheHighestSale2(), which one would perform faster? The answer is the IsToyotaTheHighestSale2().

Why? When you use Linq and get an IEnumerable there is actually no calculation, until you act on the IEnumerable. In IsToyotaTheHighestSale2(), if toyota sale is not higher than honda sale, the function does not need to calculate bmw sale. This is why the latter function run faster in general case.

Of course, above is only a very simple example. Imagine that you need to execute a function tens of thousands of times, and your Linq statement is a lot more complicated than the above example. Acting on the Enumberable later in the logic would save you running unnecessary code.

Conclusion

You can improve your application performance by acting on the IEnumerable object right when you need it, instead of getting the result in the early stage of your logic.

However, there are another cases when you should not use IEnumerable, which I will cover in the next blog.

No comments: