Parallel Power并行的力量(一个测试代码)

Parallel
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. namespace ConsoleApplication2
  9. {
  10.     class Test
  11.     {
  12.         static void Main()
  13.         {
  14.             int max = 500;
  15.             var query = Enumerable.Range(0, max)
  16.                                   .Select(SlowProjection)
  17.                                   .Where(x => x > 10)
  18.                                   .AsParallel();
  19.             Stopwatch sw = Stopwatch.StartNew();
  20.             int count = query.Count();
  21.             sw.Stop();
  22.             Console.WriteLine("Count: {0} in {1}ms", count,
  23.                               sw.ElapsedMilliseconds);
  24.  
  25.             query = Enumerable.Range(0, max)
  26.                               .AsParallel()
  27.                               .Select(SlowProjection)
  28.                               .Where(x => x > 10);
  29.             sw = Stopwatch.StartNew();
  30.             count = query.Count();
  31.             sw.Stop();
  32.             Console.WriteLine("Count: {0} in {1}ms", count,
  33.                               sw.ElapsedMilliseconds);
  34.             Console.Read();
  35.         }
  36.  
  37.         static int SlowProjection(int input)
  38.         {
  39.             Thread.Sleep(100);
  40.             return input;
  41.         }
  42.     }
  43.  
  44. }

这里比较了两种使用并行效率的比较,没有写顺序执行的原因你懂的.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值