C# Stopwatch 使用
1 static IEnumerable<int> SampleData() 2 { 3 const int arraySize = 100000000; 4 var r = new Random(); 5 return Enumerable.Range(0, arraySize).Select(x => r.Next(140)).ToList(); 6 } 7 8 static void IntroParallel() 9 { 10 var data = SampleData(); 11 12 var watch = new Stopwatch(); 13 14 watch.Start(); 15 var q1 = (from x in data 16 where Math.Log(x) < 4 17 select x).Average(); 18 watch.Stop(); 19 Console.WriteLine("sync {0}, result: {1}", watch.ElapsedMilliseconds, q1); 20 21 watch.Reset(); 22 watch.Start(); 23 var q2 = (from x in Partitioner.Create(data).AsParallel() 24 where Math.Log(x) < 4 25 select x).Average(); 26 watch.Stop(); 27 Console.WriteLine("async {0}, result: {1}", watch.ElapsedMilliseconds, q2); 28 }
鹰击长空,鱼翔浅底