惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
WordPress大学
WordPress大学
罗磊的独立博客
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Help Net Security
S
SegmentFault 最新的问题
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
D
DataBreaches.Net
雷峰网
雷峰网
GbyAI
GbyAI
宝玉的分享
宝玉的分享

博客园 - rosanshao

使用sc 命令写脚本 添加和删除服务 简单应用 Win 10激活 couchbase map reduce StartWith 测试 笔记,转 Couchbase II( View And Index) Couchbase I SqlIO优化 死锁检测 MemCached 安装笔记 Asp.Net异步编程-使用了异步,性能就提升了吗? Autofac Mvc Webapi注入笔记 Sql Server 2005/2008 SqlCacheDependency查询通知的使用总结 WCF .NET REST调用方式 WCF HelpPage 和自动根据头返回JSON XML IIS 假死状态处理 gmap jQuery插件开发 StatusCode - rosanshao - 博客园
.net 4.0 新特性 Linq 并行化处理
rosanshao · 2011-01-27 · via 博客园 - rosanshao

 //
        // 摘要:
        //     启用查询的并行化。
        //
        // 参数:
        //   source:
        //     要转换为 System.Linq.ParallelQuery<TSource> 的 System.Collections.Generic.IEnumerable<T>。
        //
        // 类型参数:
        //   TSource:
        //     source 中的元素的类型。
        //
        // 返回结果:
        //     作为要绑定到 ParallelEnumerable 扩展方法的 System.Linq.ParallelQuery<TSource> 的源。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     source 是 null 引用(在 Visual Basic 中为 Nothing)。
        public static ParallelQuery<TSource> AsParallel<TSource>(this IEnumerable<TSource> source);

首先是测试结果:

测试代码如下

static void Main(string[] args)
        {
            IEnumerable<int> numbers = Enumerable.Range(1, 1000);

            // Remove AsParallel() Method in PLINQ query to see the difference in speed
            IEnumerable<int> results = from n in numbers.AsParallel() //并行化计算 from n in numbers 非并行化计算
                                       where IsDivisibleByFive(n)
                                       select n;

            Stopwatch sw = Stopwatch.StartNew();
            IList<int> resultsList = results.ToList();
            Console.WriteLine("{0} items", resultsList.Count());
            sw.Stop();

            Console.WriteLine("It Took {0} ms", sw.ElapsedMilliseconds);

            Console.WriteLine("\nFinished...");
            Console.ReadKey(true);
        }

        static bool IsDivisibleByFive(int i)
        {
            Thread.SpinWait(2000000);
            return i % 5 == 0;
        }