From 79d4b2029458bac9d643879d0ffa4c180fab0c69 Mon Sep 17 00:00:00 2001 From: Edwin <1070342164@qq.com> Date: Sun, 17 Aug 2025 19:42:49 +0800 Subject: [PATCH] update ParallelExample --- .../HelloDotNetGuide/Program.cs | 10 +- .../异步多线程编程/ParallelExample.cs | 105 ++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 DotNetGuidePractice/HelloDotNetGuide/异步多线程编程/ParallelExample.cs diff --git a/DotNetGuidePractice/HelloDotNetGuide/Program.cs b/DotNetGuidePractice/HelloDotNetGuide/Program.cs index f5399c5..7454ad0 100644 --- a/DotNetGuidePractice/HelloDotNetGuide/Program.cs +++ b/DotNetGuidePractice/HelloDotNetGuide/Program.cs @@ -13,6 +13,14 @@ namespace HelloDotNetGuide { Console.WriteLine("欢迎来到DotNetGuide练习空间!!!"); + #region ParallelExample + + //ParallelExample.ParallelForExample(); + //ParallelExample.ParallelForEachExample(); + //ParallelExample.ParallelForCounterexample(); + + #endregion + #region ConstAndReadonlyExercise //ConstAndReadonlyExercise.UpdateApplicationNameValue(); @@ -54,7 +62,7 @@ namespace HelloDotNetGuide //LinqExercise.CountByExample(); //LinqExercise.AggregateByExample(); //LinqExercise.IndexExample(); - LinqExercise.CommonMethodsInLINQ(); + //LinqExercise.CommonMethodsInLINQ(); #endregion diff --git a/DotNetGuidePractice/HelloDotNetGuide/异步多线程编程/ParallelExample.cs b/DotNetGuidePractice/HelloDotNetGuide/异步多线程编程/ParallelExample.cs new file mode 100644 index 0000000..0c02d77 --- /dev/null +++ b/DotNetGuidePractice/HelloDotNetGuide/异步多线程编程/ParallelExample.cs @@ -0,0 +1,105 @@ +using System.Diagnostics; + +namespace HelloDotNetGuide.异步多线程编程 +{ + public class ParallelExample + { + public static void ParallelForExample() + { + var length = 1000000; + var stopwatch = Stopwatch.StartNew(); + + // 统计普通 for 循环耗时 + for (int i = 0; i < length; i++) + { + for (int j = 0; j < 1000; j++) + { + var sum = i * j; + } + } + stopwatch.Stop(); + Console.WriteLine($"普通 for 循环耗时: {stopwatch.ElapsedMilliseconds} 毫秒"); + + stopwatch.Restart(); + + // 统计 Parallel.For 循环耗时 + Parallel.For(0, length, i => + { + for (int j = 0; j < 1000; j++) + { + var sum = i * j; + } + }); + + stopwatch.Stop(); + Console.WriteLine($"Parallel.For 循环耗时: {stopwatch.ElapsedMilliseconds} 毫秒"); + Console.ReadKey(); + } + + public static void ParallelForEachExample() + { + var length = 1000000; + var numbers = Enumerable.Range(0, length).ToList(); + + var stopwatch = Stopwatch.StartNew(); + + // 统计普通 foreach 循环耗时 + foreach (var i in numbers) + { + for (int j = 0; j < 1000; j++) + { + var sum = i * j; + } + } + stopwatch.Stop(); + Console.WriteLine($"普通 foreach 循环耗时: {stopwatch.ElapsedMilliseconds} 毫秒"); + + stopwatch.Restart(); + + // 统计 Parallel.ForEach 循环耗时 + Parallel.ForEach(numbers, i => + { + for (int j = 0; j < 1000; j++) + { + var sum = i * j; + } + }); + + stopwatch.Stop(); + Console.WriteLine($"Parallel.ForEach 循环耗时: {stopwatch.ElapsedMilliseconds} 毫秒"); + Console.ReadKey(); + } + + public static void ParallelForCounterexample() + { + object lockObj = new object(); + + var stopwatch = Stopwatch.StartNew(); + long sumFor = 0; + for (int i = 0; i < 1000; i++) + { + sumFor += i; // 简单累加 + } + stopwatch.Stop(); + Console.WriteLine($"普通 for 循环: 结果 = {sumFor}, 耗时 = {stopwatch.ElapsedMilliseconds} 毫秒"); + + + stopwatch.Restart(); + + long sumParallel = 0; + Parallel.For(0, 1000, i => + { + // 因为多个线程同时修改同一个变量会导致计算结果丢失,lock 能确保每次只有一个线程可以修改,保证结果正确。 + lock (lockObj) + { + sumParallel += i; + } + }); + + stopwatch.Stop(); + Console.WriteLine($"Parallel.For: 结果 = {sumParallel}, 耗时 = {stopwatch.ElapsedMilliseconds} 毫秒"); + + Console.ReadKey(); + } + } +}