Tuesday, 11 August 2015

Foreach loop vs Parallel.ForEach

Foreach loop:
·         Iterations takes place sequentially one by one
·         foreach loop is run from a single Thread.
·         foreach loop is defined in every framework of .NET
·         Execution is slower

Parallel.ForEach:
·         Execution takes place in parallel way.
·         Parallel.ForEach uses multiple Threads.
·         Parallel.ForEach is defined in .Net 4.0 and above frameworks.
·         Execution is faster3

The following example clearly demonstrates the difference between traditional foreach loop and
Parallel.ForEach() Example.



using System;
    using System.Diagnostics;
    using System.Threading;
    using System.Threading.Tasks;
    namespace ParallelForEachExample
    {
        class Program
        {
            static void Main()
            {
                string[] colors = {
                                      "1. Red",
                                      "2. Green",
                                      "3. Blue",
                                      "4. Yellow",
                                      "5. White",
                                      "6. Black",
                                      "7. Violet",
                                      "8. Brown",
                                      "9. Orange",
                                      "10. Pink"
                                  };
                Console.WriteLine("Traditional foreach loop\n");
                //start the stopwatch for "for" loop
                var sw = Stopwatch.StartNew();
                foreach (string color in colors)
                {
                    Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(10);
                }
                Console.WriteLine("foreach loop execution time = {0} seconds\n", sw.Elapsed.TotalSeconds);
                Console.WriteLine("Using Parallel.ForEach");
                //start the stopwatch for "Parallel.ForEach"
                 sw = Stopwatch.StartNew();
                Parallel.ForEach(colors, color =>
                {
                    Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(10);
                }
                );
                Console.WriteLine("Parallel.ForEach() execution time = {0} seconds", sw.Elapsed.TotalSeconds);
                Console.Read();
            }
        }
    }




Traditional foreach loop
1. Red, Thread Id= 10
2. Green, Thread Id= 10
3. Blue, Thread Id= 10
4. Yellow, Thread Id= 10
5. White, Thread Id= 10
6. Black, Thread Id= 10
7. Violet, Thread Id= 10
8. Brown, Thread Id= 10
9. Orange, Thread Id= 10
10. Pink, Thread Id= 10
foreach loop execution time = 0.1054376 seconds


1. Red, Thread Id= 10
3. Blue, Thread Id= 11
4. Yellow, Thread Id= 11
2. Green, Thread Id= 10
5. White, Thread Id= 12
7. Violet, Thread Id= 14
9. Orange, Thread Id= 13
6. Black, Thread Id= 11
8. Brown, Thread Id= 10
10. Pink, Thread Id= 12
Parallel.ForEach() execution time = 0.055976 seconds

No comments:

Post a Comment