您的位置:首页 > 编程语言

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

2011-08-30 16:33 239 查看
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;
        }
    }

}
这里比较了两种使用并行效率的比较,没有写顺序执行的原因你懂的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: