zoukankan      html  css  js  c++  java
  • .net 4.0 新特性 Linq 并行化处理

     //
            // 摘要:
            //     启用查询的并行化。
            //
            // 参数:
            //   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;
            }

  • 相关阅读:
    不一样的图片加载方式
    赢 1000 元现金红包!助力奥运,猜金银牌数赢现金
    接入 SDK 结果翻车了?了解 SDK 的那些事
    关于 IPv6 国家有大动作啦!快来瞅瞅行动计划都说了什么~
    MySQL 那些常见的错误设计规范
    webpack 从 0 到 1 构建 vue
    PHP 网络通信底层原理分析
    内部方案汇总
    taro+vue3 引入 taro-ui-vue3
    springboot+tomcat+vue+nginx 前后端分离配置
  • 原文地址:https://www.cnblogs.com/rosanshao/p/1946302.html
Copyright © 2011-2022 走看看