Geeks With Blogs
Abhishek Anand Bits & Bytes about .net technology framework.

As most of you know LINQ provides a set of great extension methods for common functions on .NET Collections. In most cases LINQ is simply the “prettier”, abstracted out implementations of the lower level .NET language features like loops and conditionals. Even though LINQ often increases readability, it does come with a little more overhead. This additional overhead is negligible in most cases, but when performance is a primary concern in your function every millisecond counts. In these scenarios, better results can be achieved by simply writing out the loops and conditionals.

 

Let’s look at some examples:

 

Example 1:

           IEnumerable<Driver> drivers = GetDrivers();

            foreach (var driver in drivers.Where(driver => driver.DriverId > 2))

            {

                Console.WriteLine(driver.DriverId);

            }

(*Elapsed Time: ~2ms)

 

                              Vs.

 

           IEnumerable<Driver> drivers = GetDrivers();
            foreach (var driver in drivers)
            {
                if (driver.DriverId > 2)
                {
                     Console.WriteLine(driver.DriverId);
                }
 
            }

                              (*Elapsed Time: ~1ms)

 

In this example we know from the problem domain that a policy will not be allowed to have more than 10 drivers, so we don’t really have to worry about the additional 1ms overhead that using the Where() adds. Therefore we can gain the benefits of the “prettier” syntax, increasing readability and writability for the developers.

 

Example 2:

            var collection = Enumerable.Range(0, 10000000);
            foreach (var elem in collection)
            {
                if(elem <= 100 || elem >= 1000) 
                {
                     counter++;
                }
            }

                    (*Elapsed Time: ~75ms)

 

                    Vs.

 

        var collection = Enumerable.Range(0, 10000000);
        foreach (var elem in collection.Where(elem => elem <= 100 || elem >= 1000))
        {
                counter++;
        }
        

                 (*Elapsed Time: ~200ms)

 

In this scenario, the loop is going to be iterating many more times and is meant to model a case when performance is a primary concern. In this case, we see that using LINQ results in more than double the amount of time over the simple conditional.

 

Takeaways: Now, please DON’T read this as “never use LINQ”. As previously stated, it can and SHOULD be used in the vast majority of scenarios. The point being made here is that when you know performance is key you should consider sacrificing the readability and writability of LINQ for the decreased overhead. 

Posted on Monday, November 18, 2013 6:42 PM .net , linq , c# | Back to top


Comments on this post: LINQ in Performance Intensive Situations

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Abhishek Anand | Powered by: GeeksWithBlogs.net