Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 Foreach와 Parallel.Foreach의 차이점은 무엇입니까?

<시간/>

C#의 Foreach 루프는 단일 스레드에서 실행되며 처리는 하나씩 순차적으로 발생합니다. 반면 C#의 Parallel.Foreach 루프는 여러 스레드에서 실행되고 처리는 병렬 방식으로 발생합니다. 즉, 이전 항목이 완료될 때까지 기다리지 않고 한 번에 모든 항목을 반복합니다.

Parallel.Foreach의 실행은 일반적인 ForEach보다 빠릅니다. Parallel.ForEach 루프를 사용하려면 System.Threading.Tasks 네임스페이스를 가져와야 합니다.

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();
      }
   }
}

출력

위 코드의 출력은

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

위의 결과에서 Parallel.Foreach가 다른 스레드에서 실행되고 일반 foreach에 비해 시간이 덜 걸린다는 것을 알 수 있습니다.