Parallel Power并行的力量(一个测试代码)
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;
- }
- }
- }
这里比较了两种使用并行效率的比较,没有写顺序执行的原因你懂的.