What is the difference between Foreach and Parallel.Foreach in C#?

CsharpServer Side ProgrammingProgramming

Foreach loop in C# runs upon a single thread and processing takes place sequentially one by one. Whereas Parallel.Foreach loop in C# runs upon multiple threads and processing takes place in a parallel way. Which means it is looping through all items at once without waiting for the previous item to complete.

The execution of Parallel.Foreach is faster than normal ForEach. To use Parallel.ForEach loop we need to import System.Threading.Tasks namespace.

Example

 Live Demo

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace DemoApplication{
   class Demo{
      static void Main(string[] args){
         var animals = new List<string>{
            "cat", "rat", "deer", "elephant", "lion", "tiger", "dog", "pig", "buffalo", "rabbit", "horse", "cheetah", "cow", "goat", "sheep", "donkey", "zebra", "wolf", "fox", "lepoard", "monkey", "kangaroo", "girafee", "hippo"
         };
         var stopWatch = Stopwatch.StartNew();
         foreach (string animal in animals){
            Console.WriteLine($"Fruit Name: {animal}, Thread Id={Thread.CurrentThread.ManagedThreadId}");
         }
         Console.WriteLine($"foreach loop execution time = {stopWatch.Elapsed.TotalSeconds} seconds");
         var stopWatch2 = Stopwatch.StartNew();
         Parallel.ForEach(animals, animal =>{
            Console.WriteLine($"Fruit Name: {animal}, Thread Id={Thread.CurrentThread.ManagedThreadId}");
         });
         Console.WriteLine($"Parallel foreach loop execution time = {stopWatch2.Elapsed.TotalSeconds} seconds");
         Console.ReadLine();
      }
   }
}

Output

The output of the above code is

Fruit Name: cat, Thread Id= 1
Fruit Name: rat, Thread Id= 1
Fruit Name: deer, Thread Id= 1
Fruit Name: elephant, Thread Id= 1
Fruit Name: lion, Thread Id= 1
Fruit Name: tiger, Thread Id= 1
Fruit Name: dog, Thread Id= 1
Fruit Name: pig, Thread Id= 1
Fruit Name: buffalo, Thread Id= 1
Fruit Name: rabbit, Thread Id= 1
Fruit Name: horse, Thread Id= 1
Fruit Name: cheetah, Thread Id= 1
Fruit Name: cow, Thread Id= 1
Fruit Name: goat, Thread Id= 1
Fruit Name: sheep, Thread Id= 1
Fruit Name: donkey, Thread Id= 1
Fruit Name: zebra, Thread Id= 1
Fruit Name: wolf, Thread Id= 1
Fruit Name: fox, Thread Id= 1
Fruit Name: lepoard, Thread Id= 1
Fruit Name: monkey, Thread Id= 1
Fruit Name: kangaroo, Thread Id= 1
Fruit Name: girafee, Thread Id= 1
Fruit Name: hippo, Thread Id= 1
foreach loop execution time = 0.0129221 seconds
Fruit Name: cat, Thread Id= 1
Fruit Name: rat, Thread Id= 1
Fruit Name: deer, Thread Id= 1
Fruit Name: elephant, Thread Id= 1
Fruit Name: lion, Thread Id= 1
Fruit Name: tiger, Thread Id= 1
Fruit Name: pig, Thread Id= 1
Fruit Name: buffalo, Thread Id= 1
Fruit Name: dog, Thread Id= 3
Fruit Name: sheep, Thread Id= 3
Fruit Name: donkey, Thread Id= 3
Fruit Name: rabbit, Thread Id= 1
Fruit Name: goat, Thread Id= 5
Fruit Name: fox, Thread Id= 5
Fruit Name: lepoard, Thread Id= 5
Fruit Name: cow, Thread Id= 4
Fruit Name: hippo, Thread Id= 4
Fruit Name: kangaroo, Thread Id= 9
Fruit Name: girafee, Thread Id= 6
Fruit Name: monkey, Thread Id= 5
Fruit Name: horse, Thread Id= 1
Fruit Name: cheetah, Thread Id= 1
Fruit Name: zebra, Thread Id= 3
Fruit Name: wolf, Thread Id= 3
Parallel foreach loop execution time = 0.0092988 seconds

From the above result we can see, Parallel.Foreach executes in different threads and takes less time compared to normal foreach.

raja
Published on 04-Aug-2020 10:55:41
Advertisements