日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

IEnumerable.OrderBy().First() 在 .netcore 3.1 中是否做了优化?

發布時間:2023/12/4 72 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IEnumerable.OrderBy().First() 在 .netcore 3.1 中是否做了优化? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

咨詢區

  • Matthew Watson

我記得 .net core 中有一個版本對下面的場景做了一個優化,代碼如下:

int?smallest?=?new[]{?7,?2,?4,?6,?0,?1,?6,?9,?8?}.OrderBy(i?=>?i).First();

在很早之前它的時間復雜度是 O(N.Log(N)),現在已經優化成 O(N) 了。

為了驗證,我用 .NET Core 和 .NET Framework 來跑下面的這段代碼,參考如下:

using?System; using?System.Collections.Generic; using?System.Linq;namespace?Demo {static?class?Program{static?void?Main(){int[]?test?=?{?7,?2,?4,?6,?0,?1,?6,?9,?8?};var?comparer?=?new?Comparer();var?_?=?test.OrderBy(i?=>?i,?comparer).First();}}class?Comparer?:?IComparer<int>{public?int?Compare(int?x,?int?y){Console.WriteLine($"Comparing?{x}?with?{y}");return?x.CompareTo(y);}} }

如果你想試試的話,可以參考如下兩個在線工具。

.NET Framework: https://dotnetfiddle.net/XItXYL

.NET Core: https://dotnetfiddle.net/swlc0O

  • .NET Framework 4.8

  • Comparing?0?with?7 Comparing?0?with?8 Comparing?0?with?9 Comparing?0?with?6 Comparing?0?with?1 Comparing?0?with?0 Comparing?0?with?2 Comparing?0?with?6 Comparing?0?with?4 Comparing?0?with?2 Comparing?0?with?0 Comparing?7?with?2 Comparing?7?with?4 Comparing?7?with?6 Comparing?7?with?7 Comparing?7?with?8 Comparing?7?with?9 Comparing?7?with?6 Comparing?7?with?1 Comparing?7?with?7 Comparing?7?with?1 Comparing?9?with?7 Comparing?9?with?9 Comparing?9?with?8 Comparing?7?with?7 Comparing?7?with?8 Comparing?7?with?7 Comparing?6?with?2 Comparing?6?with?4 Comparing?6?with?6 Comparing?6?with?1 Comparing?6?with?6 Comparing?6?with?6 Comparing?6?with?1 Comparing?6?with?6 Comparing?6?with?6 Comparing?4?with?2 Comparing?4?with?4 Comparing?4?with?1 Comparing?2?with?2 Comparing?2?with?1
  • .NET Core 3.1

  • Comparing?2?with?7 Comparing?4?with?2 Comparing?6?with?2 Comparing?0?with?2 Comparing?1?with?0 Comparing?6?with?0 Comparing?9?with?0 Comparing?8?with?0

    最后提一下,大家記得在哪里看到這樣的文檔記載嗎?不是官方的就不要發了。

    回答區

    • Matthew Watson

    確實,對這個進行優化的文檔說明是有的,可以參考下 Github: ?https://github.com/dotnet/runtime/issues/14867 上的這個討論和最后的編碼實現。

    點評區

    由 O(N.Log(N)) 變成 O(LogN),這確實是一個非常大的性能提升,我們的基礎庫真的是越來越強大 🐂👃。

    總結

    以上是生活随笔為你收集整理的IEnumerable.OrderBy().First() 在 .netcore 3.1 中是否做了优化?的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。