Home > Backend Development > C++ > How Does Parallel.ForEach Improve Performance Compared to a Traditional Foreach Loop in .NET?

How Does Parallel.ForEach Improve Performance Compared to a Traditional Foreach Loop in .NET?

Susan Sarandon
Release: 2025-01-06 00:36:38
Original
696 people have browsed it

How Does Parallel.ForEach Improve Performance Compared to a Traditional Foreach Loop in .NET?

Parallel Processing with Parallel.ForEach

Parallel programming enhances application performance by harnessing multiple processor cores. Parallel.ForEach is a powerful construct that simplifies parallel processing in .NET applications.

Foreach Loop vs. Parallel.ForEach

The traditional foreach loop performs iterations sequentially on a single thread. Parallel.ForEach, on the other hand, distributes the workload across multiple threads, enabling simultaneous execution of iterations.

Characteristics of Foreach Loops:

  • Sequential execution of iterations
  • Single thread utilization
  • Ideal for quick operations where threading overhead is not significant

Characteristics of Parallel.ForEach:

  • Parallel execution of iterations
  • Multiple threads utilization
  • Beneficial for slow operations where parallelization improves performance

Converting to Parallel.ForEach

To convert the given code snippet to Parallel.ForEach:

Parallel.ForEach(list_lines, line =>
{
    // My Stuff
});
Copy after login

Example

The following example demonstrates the difference between a foreach loop and Parallel.ForEach:

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ParallelForEachExample
{
    class Program
    {
        static void Main()
        {
            var lines = File.ReadAllLines("proxylist.txt");
            var list_lines = new List<string>(lines);

            Console.WriteLine("Traditional foreach loop");
            var watch = Stopwatch.StartNew();
            foreach (var line in list_lines)
            {
                Console.WriteLine(line);
                Thread.Sleep(10);
            }
            watch.Stop();
            Console.WriteLine($"foreach loop execution time: {watch.ElapsedMilliseconds} ms");

            Console.WriteLine("Using Parallel.ForEach");
            watch = Stopwatch.StartNew();
            Parallel.ForEach(list_lines, line =>
            {
                Console.WriteLine(line);
                Thread.Sleep(10);
            });
            watch.Stop();
            Console.WriteLine($"Parallel.ForEach() execution time: {watch.ElapsedMilliseconds} ms");
        }
    }
}
Copy after login

Output

The output will demonstrate that Parallel.ForEach executes the iterations in parallel, reducing the overall processing time compared to the traditional foreach loop.

The above is the detailed content of How Does Parallel.ForEach Improve Performance Compared to a Traditional Foreach Loop in .NET?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template