在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 static void Main(string[] args) 2 { 3 var dt = DateTime.Now; 4 var rand = new Random(DateTime.Now.Millisecond); 5 for (int i = 0; i < 20; i++) 6 { 7 Thread.Sleep(rand.Next(100, 1000)); 8 } 9 var standerTime = (DateTime.Now - dt).TotalMilliseconds; 10 Console.WriteLine("标准的for循环,耗时{0}毫秒", standerTime); 11 dt = DateTime.Now; 12 Parallel.For(0, 20, i => 13 { 14 Thread.Sleep(rand.Next(100, 1000)); 15 }); 16 var parallelTime = (DateTime.Now - dt).TotalMilliseconds; 17 Console.WriteLine("并行的for循环,耗时{0}毫秒", parallelTime); 18 Console.WriteLine(); 19 if (standerTime <= parallelTime) 20 { 21 Console.WriteLine("----------标准的for循环节省{0}毫秒----------", (parallelTime - standerTime)); 22 } 23 else 24 { 25 Console.WriteLine("----------并行的for循环节省{0}毫秒----------", (standerTime - parallelTime)); 26 } 27 Console.ReadKey(); 28 } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论