久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    1. <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 class="qwawimqqmiuu" id='oBodL'><tfoot id='oBodL'></tfoot><dl id='oBodL'><fieldset id='oBodL'></fieldset></dl></div>
    2. <tfoot id='oBodL'></tfoot>
    3. <small id='oBodL'></small><noframes id='oBodL'>

    4. <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 中的聚合與總和性能

        Aggregate vs Sum Performance in LINQ(LINQ 中的聚合與總和性能)
          <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 class="qwawimqqmiuu" 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'>

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

                • 本文介紹了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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What are good algorithms for vehicle license plate detection?(車牌檢測有哪些好的算法?)
                  onClick event for Image in Unity(Unity中圖像的onClick事件)
                  Running Total C#(運行總 C#)
                  Deleting a directory when clicked on a hyperlink with JAvascript.ASP.NET C#(單擊帶有 JAvascript.ASP.NET C# 的超鏈接時刪除目錄)
                  asp.net listview highlight row on click(asp.net listview 在單擊時突出顯示行)
                  Calling A Button OnClick from a function(從函數調用按鈕 OnClick)

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

                        <tfoot id='nLVMl'></tfoot>
                      • <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 class="qwawimqqmiuu" id='nLVMl'><tfoot id='nLVMl'></tfoot><dl id='nLVMl'><fieldset id='nLVMl'></fieldset></dl></div>
                          <tbody id='nLVMl'></tbody>

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

                          1. 主站蜘蛛池模板: 午夜丰满少妇一级毛片 | 91视频三区 | 二区中文字幕 | 伊人色综合久久久天天蜜桃 | 激情一区二区三区 | 色播视频在线观看 | 午夜精品一区二区三区在线视频 | 精品一二三区在线观看 | 亚洲国产精品一区二区三区 | 国产区高清 | 欧美色性 | 天天插天天操 | 97精品超碰一区二区三区 | 精品婷婷 | 一区二区中文 | 国产视频精品免费 | 九一视频在线观看 | 91久久久久久久久 | 久久免费精品视频 | 欧美精品福利 | 婷婷激情综合 | 久久精品久久久久久 | 精品美女久久久 | 国产精品久久久久久久久久久新郎 | 欧美在线视频一区 | 日韩成人精品在线观看 | 国产成人午夜高潮毛片 | 久久亚洲一区 | 国产精品一二区 | 欧美不卡一区二区三区 | 婷婷激情五月网 | 亚洲国产精品一区二区第一页 | 精品国产乱码久久久久久闺蜜 | 一区二区三区av | 国产精品成人一区二区三区 | 久久男人 | 在线亚洲欧美 | 日干夜操 | 成人妇女免费播放久久久 | 玖玖精品视频 | 在线观看亚洲专区 |