Thursday, October 27, 2016

Which Loop Runs Faster in NET?

Problem: Which Looping structure runs faster?

Selected Looping Structures :

  • While Loop
  • Do-While Loop
  • For Loop
  • Lamda For-Each Loop
  • Parallel For Loop
  • Parallel For-Each Loop

    Test Code : (Console Application)

    Main Method :
    static void Main(string[] args)
    {
     int[] someData = new int[] {465,545,674,354 };
       Stopwatch timer = new Stopwatch();
       timer.Start();
       ProcessUsingWhileLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using While Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingDoWhileLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using Do-While Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingForLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using For Loop :"+timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingLamdaForEachLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using Lamda ForEach Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingParallelForLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using Parallel For Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingParallelForEachLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using Parallel ForEach Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       Console.ReadLine();
     }

    ProcessUsingWhileLoop Method :
    static void ProcessUsingWhileLoop(int[] data)
    {
        int n = 0;
        while ( n < data.Length)
        {
            System.Threading.Thread.Sleep(data[n]);
            n++;
        }
    }
    

    ProcessUsingDoWhileLoop Method :
    static void ProcessUsingDoWhileLoop(int[] data)
    {
        int n = 0;
        do
        {
            System.Threading.Thread.Sleep(data[n]);
            n++;
        }
        while (n < data.Length);
    }
    

    ProcessUsingForLoop Method :
    static void ProcessUsingForLoop(int[] data)
    {
        for (int n = 0; n < data.Length; n++ )
        {
            System.Threading.Thread.Sleep(data[n]);
        }
    }
    

    ProcessUsingLamdaForEachLoop Method :
    static void ProcessUsingLamdaForEachLoop(int[] data)
    {
       data.ToList().ForEach(
            n => System.Threading.Thread.Sleep(n));
    }
    

    ProcessUsingParallelForLoop Method :
    static void ProcessUsingLamdaForEachLoop(int[] data)
    {
       Parallel.For(0, data.Length,
            n => 
            {
                System.Threading.Thread.Sleep(data[n]);
            });
    }
    

    ProcessUsingParallelForEachLoop Method :
    static void ProcessUsingParallelForEachLoop(int[] data)
    {
        Parallel.ForEach(data, 
             n =>
             {
                System.Threading.Thread.Sleep(data[n]);
             });
    }
    


    Results :
    (built-in Debug Mode and run from Visual Studio)

    Process Using While Loop: 2039
    Process Using Do-While Loop: 2041
    Process Using For Loop: 2064
    Process Using Lamda ForEach Loop: 2074
    Process Using Parallel For Loop: 984
    Process Using Parallel ForEach Loop: 870

    (built-in Release Mode ran from executable)
    Process Using While Loop: 2046
    Process Using Do-While Loop: 2040
    Process Using For Loop: 2039
    Process Using Lamda ForEach Loop: 2046
    Process Using Parallel For Loop: 838
    Process Using Parallel ForEach Loop: 837


    Conclusion :
    Based on the data collected, below is the list of the fastest looping structure to a slower one.
    1. Parallel ForEach Loop
    2. Parallel For Loop
    3. For Loop
    4. While Loop
    5. Do-While Loop
    6. Lamda ForEach

    No comments:

    Post a Comment