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!!!

1 comment:

  1. Hello Blogger,
    Luxury Golden Triangle Tour package is specially designed tour package by Delhi Agra Jaipur Yatra which is connecting three famous heritage cities of India that are nation's capitral Delhi, city of love - Agra and Pink city Jaipur. If you are planning a tour or visiting first time to India for exploring incredible India then these cities are must visit destinations. In luxury Golden Triangle Tour your stay in the best hotels of every city. Hotels include are Oberoi, Leela, Taj, 5 star hotels and other heritage hotelsat best price in the market. Car rental services are also available for exploring the cities of Udaipur, Ranthambore and other famous cities. If you want to add some more places in your trip, you can Customize Your Tour also. So plan your vacations with Delhi Agra Jaipur Yatra which make your trip more comfortable and hassle free tour.
    India Trip Designer
    www.indiatripdesigner.com
    +91-9837332533

    ReplyDelete