VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C# Parallel类For循环与普通For循环耗时性能

复制代码
 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         }
复制代码

相关教程