<tfoot id='iXOaV'></tfoot>
      <i id='iXOaV'><tr id='iXOaV'><dt id='iXOaV'><q id='iXOaV'><span id='iXOaV'><b id='iXOaV'><form id='iXOaV'><ins id='iXOaV'></ins><ul id='iXOaV'></ul><sub id='iXOaV'></sub></form><legend id='iXOaV'></legend><bdo id='iXOaV'><pre id='iXOaV'><center id='iXOaV'></center></pre></bdo></b><th id='iXOaV'></th></span></q></dt></tr></i><div id='iXOaV'><tfoot id='iXOaV'></tfoot><dl id='iXOaV'><fieldset id='iXOaV'></fieldset></dl></div>

        • <bdo id='iXOaV'></bdo><ul id='iXOaV'></ul>

        <small id='iXOaV'></small><noframes id='iXOaV'>

        <legend id='iXOaV'><style id='iXOaV'><dir id='iXOaV'><q id='iXOaV'></q></dir></style></legend>

      1. LINQ 中的聚合与总和性能

        时间:2023-06-05
              <i id='oBodL'><tr id='oBodL'><dt id='oBodL'><q id='oBodL'><span id='oBodL'><b id='oBodL'><form id='oBodL'><ins id='oBodL'></ins><ul id='oBodL'></ul><sub id='oBodL'></sub></form><legend id='oBodL'></legend><bdo id='oBodL'><pre id='oBodL'><center id='oBodL'></center></pre></bdo></b><th id='oBodL'></th></span></q></dt></tr></i><div id='oBodL'><tfoot id='oBodL'></tfoot><dl id='oBodL'><fieldset id='oBodL'></fieldset></dl></div>
                <tbody id='oBodL'></tbody>
              <tfoot id='oBodL'></tfoot>
            1. <small id='oBodL'></small><noframes id='oBodL'>

            2. <legend id='oBodL'><style id='oBodL'><dir id='oBodL'><q id='oBodL'></q></dir></style></legend>
                  <bdo id='oBodL'></bdo><ul id='oBodL'></ul>

                  本文介绍了LINQ 中的聚合与总和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  求和的三种不同实现 IEnumerable <下面给出了 int> source 以及 source 有 10,000 个整数所用的时间.

                  Three different implementations of finding the sum of an IEnumerable < int> source are given below along with the time taken when the source has 10,000 integers.

                  source.Aggregate(0, (result, element) => result + element);  
                  

                  需要 3 毫秒

                  source.Sum(c => c);
                  

                  需要 12 毫秒

                  source.Sum();
                  

                  需要 1 毫秒

                  我想知道为什么第二个实现比第一个贵四倍.不应该和第三个实现一样吗?

                  I am wondering why the second implementation is four times more expensive than the first one. Shouldn't it be same as the third implementation.

                  推荐答案

                  注意:我的电脑运行的是 .Net 4.5 RC,所以我的结果可能会受此影响.

                  Note: My computer is running .Net 4.5 RC, so it's possible that my results are affected by this.

                  测量只执行一次方法所花费的时间通常不是很有用.它很容易被 JIT 编译之类的东西所支配,而这些东西在实际代码中并不是真正的瓶颈.因此,我测量了每个方法的执行次数为 100 次(在没有附加调试器的发布模式下).我的结果是:

                  Measuring the time it takes to execute a method just once is usually not very useful. It can be easily dominated by things like JIT compilation, which are not actual bottlenecks in real code. Because of this, I measured executing each method 100× (in Release mode without debugger attached). My results are:

                  • Aggregate():9 毫秒
                  • Sum(lambda):12 毫秒
                  • Sum():6 毫秒
                  • Aggregate(): 9 ms
                  • Sum(lambda): 12 ms
                  • Sum(): 6 ms

                  Sum() 最快这一事实并不令人惊讶:它包含一个没有任何委托调用的简单循环,这非常快.Sum(lambda)Aggregate() 之间的差异并不像您测量的那样突出,但它仍然存在.可能是什么原因?我们来看看这两种方法的反编译代码:

                  The fact that Sum() is the fastest is not surprising: it contains a simple loop without any delegate invocations, which is really fast. The difference between Sum(lambda) and Aggregate() is not nearly as prominent as what you measured, but it's still there. What could be the reason for it? Let's look at decompiled code for the two methods:

                  public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)
                  {
                      if (source == null)
                          throw Error.ArgumentNull("source");
                      if (func == null)
                          throw Error.ArgumentNull("func");
                  
                      TAccumulate local = seed;
                      foreach (TSource local2 in source)
                          local = func(local, local2);
                      return local;
                  }
                  
                  public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
                  {
                      return source.Select<TSource, int>(selector).Sum();
                  }
                  

                  如您所见,Aggregate() 使用循环,但 Sum(lambda) 使用 Select(),后者又使用迭代器.使用迭代器意味着有一些开销:创建迭代器对象和(可能更重要的是)为每个项目多调用一次方法.

                  As you can see, Aggregate() uses a loop but Sum(lambda) uses Select(), which in turn uses an iterator. And using an iterator means there is some overhead: creating the iterator object and (probably more importantly) one more method invocation for each item.

                  让我们通过编写我们自己的 Sum(lambda) 两次来验证使用 Select() 实际上是原因,一次使用 Select(),它的行为应该与框架中的 Sum(lambda) 相同,并且一次不使用 Select():

                  Let's verify that using Select() is actually the reason by writing our own Sum(lambda) twice, once using Select(), which should behave the same as Sum(lambda) from the framework, and once without using Select():

                  public static int SlowSum<T>(this IEnumerable<T> source, Func<T, int> selector)
                  {
                      return source.Select(selector).Sum();
                  }
                  
                  public static int FastSum<T>(this IEnumerable<T> source, Func<T, int> selector)
                  {
                      if (source == null)
                          throw new ArgumentNullException("source");
                      if (selector == null)
                          throw new ArgumentNullException("selector");
                  
                      int num = 0;
                      foreach (T item in source)
                          num += selector(item);
                      return num;
                  }
                  

                  我的测量结果证实了我的想法:

                  My measurements confirm what I thought:

                  • SlowSum(lambda):12 毫秒
                  • FastSum(lambda):9 毫秒
                  • SlowSum(lambda): 12 ms
                  • FastSum(lambda): 9 ms

                  这篇关于LINQ 中的聚合与总和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:来自不同格式日期的总小时数.C# 下一篇:我怎样才能得到列“pieces"的总和?在数据表中

                  相关文章

                  最新文章

                      <bdo id='nLVMl'></bdo><ul id='nLVMl'></ul>
                  1. <legend id='nLVMl'><style id='nLVMl'><dir id='nLVMl'><q id='nLVMl'></q></dir></style></legend>

                    <tfoot id='nLVMl'></tfoot>
                  2. <i id='nLVMl'><tr id='nLVMl'><dt id='nLVMl'><q id='nLVMl'><span id='nLVMl'><b id='nLVMl'><form id='nLVMl'><ins id='nLVMl'></ins><ul id='nLVMl'></ul><sub id='nLVMl'></sub></form><legend id='nLVMl'></legend><bdo id='nLVMl'><pre id='nLVMl'><center id='nLVMl'></center></pre></bdo></b><th id='nLVMl'></th></span></q></dt></tr></i><div id='nLVMl'><tfoot id='nLVMl'></tfoot><dl id='nLVMl'><fieldset id='nLVMl'></fieldset></dl></div>
                  3. <small id='nLVMl'></small><noframes id='nLVMl'>