日期:2014-05-18 浏览次数:20781 次
class Program { static void Main(string[] args) { Stopwatch timer1 = new Stopwatch(); Stopwatch timer2 = new Stopwatch(); timer1.Start(); int max=10000000; ConcurrentQueue<int> intQueue = new ConcurrentQueue<int>(); ConcurrentQueue<int> intQueueParallel = new ConcurrentQueue<int>(); for (int i = 0; i < max; i++) { intQueue.Enqueue(i); } //List< int> intList=intQueue.ToList<int>(); timer1.Stop(); Console.WriteLine("普通循环共耗时:" + timer1.Elapsed.TotalSeconds + "秒"); timer2.Start(); Parallel.For(0, max, (i) => intQueueParallel.Enqueue(i)); //List<int> intListParallel = intQueueParallel.ToList<int>(); timer2.Stop(); Console.WriteLine("并行共耗时:"+timer2.Elapsed.TotalSeconds+"秒"); Console.ReadKey(); } }