Tuesday, September 25, 2012

Comparison of Execution Time taken by LINQ, FOR loop and FOREACH loop

While developing any kind of web/windows application we come across a point where we are supposed to filter records based on some criteria from bunch of data. And to define logic we may use LINQ query or FOREACH or FOR looping structure without considering the amount of data and execution time for obtaining result set.

Let’s consider that we developing a utility application/business logic which processes data. Below is demo application to check execution time of LINQ queries, FOREACH and FOR loop.

Here I am generating ONE CRORE random numbers between 0 to 100 and adding it to list and calculating occurrences of “20” number from the random number generated list.

static void Main(string[] args)
        {
            //Generating ONE CRORE random numbers
            List<int> data = new List<int>();
            System.Random rnd = new Random();
            for (int i = 0; i <= 10000000; i++)
            {
                data.Add(rnd.Next(100));
            }

            //Filtering by different methods
            Console.WriteLine("Execution time using different methods." + Environment.NewLine);
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            int count = 0;

            //------------------------------------------------
            //Method - 1: Using LINQ Query
            sw.Start();
            
            count = (from d in data
                     where d == 20
                     select d).Count();

            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds + " milliseconds - Using LINQ query");

            sw.Reset();

            //------------------------------------------------
            //Method - 2: Using LINQ lambda expression            
            sw.Start();
            
            count = data.Where(c => c == 20).Count();

            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds + " milliseconds - Using LINQ Lambda expression");

            sw.Reset();            

            //------------------------------------------------
            //Method - 3: Using FOR loop
            count = 0;
            sw.Start();     
       
            for (int i = 0; i < data.Count; i++)            
                if (data[i] == 20)
                    count++;            
            
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds + " milliseconds - Using For loop");

            sw.Reset();  

            //------------------------------------------------
            //Method - 4: Using FOREACH loop
            count = 0;
            sw.Start();

            foreach (var item in data)
                if (item == 20)
                    count++;

            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds + " milliseconds - Using Foreach loop");           
            Console.Read();
        }


In above example we used four different methods to calculate occurrences of number “20” in random number list:
  • Method – 1: Using LINQ query 
  • Method – 2: Using LINQ lambda expression 
  • Method – 3: Using FOR loop 
  • Method – 4: Using FOREACH loop 

And from the above output we can see that LINQ query creates more overhead with compared to looping controls rather FOREACH loop.

Hence, lesser execution time more faster and optimized output.

Learn by diving in Programming Ocean...
Happy Programming!!!

No comments:

Post a Comment