La boucle Parallel.ForEach en C# s'exécute sur plusieurs threads et le traitement s'effectue en parallèle. Les boucles Parallel.ForEach ne sont pas une fonctionnalité de base de C# et sont disponibles à partir de C# 4.0 et versions ultérieures. Pour utiliser la boucle Parallel.ForEach, nous devons importer l'espace de noms System.Threading.Tasks dans la directive using.
La boucle Foreach en C# s'exécute sur un seul thread et le traitement s'effectue un par un dans l'ordre. Les boucles Foreach sont une fonctionnalité fondamentale de C#, disponible à partir de C# 1.0. Dans la plupart des cas, il fonctionne plus lentement que Parallel.Foreach.
static void Main(string[] args){ List<string> alphabets = new List<string>(); alphabets.Add("A"); alphabets.Add("B"); alphabets.Add("C"); alphabets.Add("D"); alphabets.Add("E"); alphabets.Add("F"); alphabets.Add("G"); alphabets.Add("H"); alphabets.Add("I"); alphabets.Add("J"); alphabets.Add("K"); alphabets.Add("L"); alphabets.Add("M"); alphabets.Add("N"); alphabets.Add("O"); alphabets.Add("P"); alphabets.Add("Q"); alphabets.Add("R"); alphabets.Add("S"); alphabets.Add("T"); alphabets.Add("U"); alphabets.Add("V"); alphabets.Add("W"); alphabets.Add("X"); alphabets.Add("Y"); alphabets.Add("Z"); Console.WriteLine("Printing list using foreach loop"); var stopWatch = Stopwatch.StartNew(); foreach (string alphabet in alphabets){ Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet, Thread.CurrentThread.ManagedThreadId); } Console.WriteLine("foreach loop execution time = {0} seconds", stopWatch.Elapsed.TotalSeconds); Console.WriteLine("Printing list using Parallel.ForEach"); stopWatch = Stopwatch.StartNew(); Parallel.ForEach(alphabets, alphabet => { Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet, Thread.CurrentThread.ManagedThreadId); } ); Console.WriteLine("Parallel.ForEach() execution time = {0} seconds", stopWatch.Elapsed.TotalSeconds); Console.Read(); Console.ReadLine(); }
Utiliser la boucle foreach pour imprimer la liste
alphabet Name: A, Thread Id= 1 alphabet Name: B, Thread Id= 1 alphabet Name: C, Thread Id= 1 alphabet Name: D, Thread Id= 1 alphabet Name: E, Thread Id= 1 alphabet Name: F, Thread Id= 1 alphabet Name: G, Thread Id= 1 alphabet Name: H, Thread Id= 1 alphabet Name: I, Thread Id= 1 alphabet Name: J, Thread Id= 1 alphabet Name: K, Thread Id= 1 alphabet Name: L, Thread Id= 1 alphabet Name: M, Thread Id= 1 alphabet Name: N, Thread Id= 1 alphabet Name: O, Thread Id= 1 alphabet Name: P, Thread Id= 1 alphabet Name: Q, Thread Id= 1 alphabet Name: R, Thread Id= 1 alphabet Name: S, Thread Id= 1 alphabet Name: T, Thread Id= 1 alphabet Name: U, Thread Id= 1 alphabet Name: V, Thread Id= 1 alphabet Name: W, Thread Id= 1 alphabet Name: X, Thread Id= 1 alphabet Name: Y, Thread Id= 1 alphabet Name: Z, Thread Id= 1 foreach loop execution time = 0.0223421 seconds
Utiliser Parallel.ForEach pour imprimer la liste
alphabet Name: A, Thread Id= 1 alphabet Name: G, Thread Id= 4 alphabet Name: H, Thread Id= 4 alphabet Name: I, Thread Id= 4 alphabet Name: J, Thread Id= 4 alphabet Name: K, Thread Id= 4 alphabet Name: L, Thread Id= 4 alphabet Name: N, Thread Id= 4 alphabet Name: O, Thread Id= 4 alphabet Name: P, Thread Id= 4 alphabet Name: Q, Thread Id= 4 alphabet Name: Y, Thread Id= 6 alphabet Name: Z, Thread Id= 6 alphabet Name: D, Thread Id= 6 alphabet Name: E, Thread Id= 6 alphabet Name: F, Thread Id= 6 alphabet Name: T, Thread Id= 6 alphabet Name: U, Thread Id= 6 alphabet Name: V, Thread Id= 6 alphabet Name: R, Thread Id= 4 alphabet Name: M, Thread Id= 5 alphabet Name: S, Thread Id= 7 alphabet Name: B, Thread Id= 1 alphabet Name: C, Thread Id= 1 alphabet Name: W, Thread Id= 6 alphabet Name: X, Thread Id= 6 Parallel.ForEach() execution time = 0.0559777 seconds
Limiter le parallélisme en parallèle.foreach
static class Program{ static void Main(string[] args){ List<string> alphabets = new List<string>(); alphabets.Add("A"); alphabets.Add("B"); alphabets.Add("C"); alphabets.Add("D"); alphabets.Add("E"); alphabets.Add("F"); alphabets.Add("G"); alphabets.Add("H"); alphabets.Add("I"); alphabets.Add("J"); alphabets.Add("K"); alphabets.Add("L"); alphabets.Add("M"); alphabets.Add("N"); alphabets.Add("O"); alphabets.Add("P"); alphabets.Add("Q"); alphabets.Add("R"); alphabets.Add("S"); alphabets.Add("T"); alphabets.Add("U"); alphabets.Add("V"); alphabets.Add("W"); alphabets.Add("X"); alphabets.Add("Y"); alphabets.Add("Z"); Parallel.ForEach( alphabets, new ParallelOptions { MaxDegreeOfParallelism = 2 }, alphabet => { Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet, Thread.CurrentThread.ManagedThreadId); } ); } }
alphabet Name: N, Thread Id= 4 alphabet Name: O, Thread Id= 4 alphabet Name: P, Thread Id= 4 alphabet Name: A, Thread Id= 1 alphabet Name: B, Thread Id= 1 alphabet Name: C, Thread Id= 1 alphabet Name: Q, Thread Id= 4 alphabet Name: R, Thread Id= 4 alphabet Name: S, Thread Id= 4 alphabet Name: T, Thread Id= 4 alphabet Name: U, Thread Id= 4 alphabet Name: V, Thread Id= 4 alphabet Name: W, Thread Id= 4 alphabet Name: X, Thread Id= 4 alphabet Name: Y, Thread Id= 4 alphabet Name: Z, Thread Id= 4 alphabet Name: H, Thread Id= 4 alphabet Name: I, Thread Id= 4 alphabet Name: J, Thread Id= 4 alphabet Name: K, Thread Id= 4 alphabet Name: D, Thread Id= 1 alphabet Name: L, Thread Id= 4 alphabet Name: E, Thread Id= 1 alphabet Name: F, Thread Id= 1 alphabet Name: G, Thread Id= 1 alphabet Name: M, Thread Id= 4
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!