Parallel
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace ConsoleApplication2
- {
- class Test
- {
- static void Main()
- {
- int max = 500;
- var query = Enumerable.Range(0, max)
- .Select(SlowProjection)
- .Where(x => x > 10)
- .AsParallel();
- Stopwatch sw = Stopwatch.StartNew();
- int count = query.Count();
- sw.Stop();
- Console.WriteLine("Count: {0} in {1}ms", count,
- sw.ElapsedMilliseconds);
- query = Enumerable.Range(0, max)
- .AsParallel()
- .Select(SlowProjection)
- .Where(x => x > 10);
- sw = Stopwatch.StartNew();
- count = query.Count();
- sw.Stop();
- Console.WriteLine("Count: {0} in {1}ms", count,
- sw.ElapsedMilliseconds);
- Console.Read();
- }
- static int SlowProjection(int input)
- {
- Thread.Sleep(100);
- return input;
- }
- }
- }
这里比较了两种使用并行效率的比较,没有写顺序执行的原因你懂的.
本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2011/08/30/2159753.html,如需转载请自行联系原作者