自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

對《LINQ能不能用系列1數(shù)組篩選效率對比》的幾個問題

開發(fā) 后端
我們看到了一個討論LINQ到底能不能用的系列文章,但也同時看到了一篇反駁的文章。特同時發(fā)布,供大家討論。

引用原文:LINQ能不能用系列(一)數(shù)組篩選效率對比

錯誤一:作為對比測試,測試數(shù)組應該為同一個,否則測試數(shù)據(jù)沒有可比性

錯誤二:對比組中對List的使用不對,List默認創(chuàng)建的數(shù)組大小為4,每次增長為4,那么這里就會反復重新創(chuàng)建新的數(shù)組次數(shù)為log10000000次左右當然會比Linq慢很多

錯誤三:面對Linq接近0毫秒的處理能力,稍微有點經(jīng)驗的同學就知道這是不可能的,除非是很強很強的計算機,至于為什么后面給出答案,總之linq查詢里肯定有貓膩,直接調用tolist()強制返回結果再說;//這里Stone W在評論中對ToList有質疑,我之所以ToList是為了和第二組進行對比,因為第二組得到的結果是一個List,我很奇怪,這次的對比測試到底是為了測試得到兩個結果集的算法對比呢還是測試Count算法的對比呢?如果是前者,一個拿到的是IEnumerable的對象一個是List對象,牛跟鬧鐘怎么對比哪個跑的快呢?也只有在調用ToList的時候才會真正執(zhí)行Linq的算法也就是下面的嵌套類WhereListIterator;當然如果是為了進行Count對比的話那么對比組二中的算法真的有點拙劣,我想不會有誰會用方法二來統(tǒng)計。

下面是修改了如上三個明顯錯誤后的代碼,如果哪位同學有補充歡迎留言:

  1. [Fact]  
  2. public void LinqTest()  
  3. {  
  4.     TestLinq(1);  
  5.     TestLinq(2);  
  6.     TestLinq(3);  
  7. }  
  8.  
  9. public void TestLinq(int time)  
  10. {  
  11.     const int listCount = 10000000; // 數(shù)組長度  
  12.     Random random = new Random(); // 數(shù)據(jù)隨機構建值  
  13.  
  14.     // 數(shù)組構建   
  15.     List<int> list1 = new List<int>();  
  16.     for (int i = 0; i < listCount; i++)  
  17.     {  
  18.         list1.Add(random.Next(10000));  
  19.     }  
  20.  
  21.     // 效率測試內容:提取數(shù)組中數(shù)值大于的100的數(shù)組  
  22.  
  23.     // LINQ 測試  
  24.     Stopwatch linq_Stopwatch = new Stopwatch();  
  25.     linq_Stopwatch.Start();  
  26.  
  27.     var linqList = (from num in list1  
  28.                     where num > 100  
  29.                     select num).ToList();  
  30.     linq_Stopwatch.Stop();  
  31.     // 普通方式 測試  
  32.     Stopwatch before_Stopwatch = new Stopwatch();  
  33.     before_Stopwatch.Start();  
  34.  
  35.     List<int> beforeList = new List<int>(10000000);  
  36.     for (int i = 0; i < list1.Count(); i++)  
  37.     {  
  38.         if (list1[i] > 100)  
  39.             beforeList.Add(list1[i]);  
  40.     }  
  41.     before_Stopwatch.Stop();  
  42.  
  43.  
  44.     Console.WriteLine(  
  45.         String.Format("第{0}次測試,測試:{5}條數(shù)據(jù)。\n\r \t LINQ用時:{1}毫秒,篩選了{2}條數(shù)據(jù)。\n\r\t 普通用時:{3}毫秒,篩選了{4}條數(shù)據(jù)。\r\n",  
  46.                       time, linq_Stopwatch.ElapsedMilliseconds, linqList.Count(),  
  47.                       before_Stopwatch.ElapsedMilliseconds, beforeList.Count(), listCount));  
  48.  

測試結果:

第1次測試,測試:10000000條數(shù)據(jù)。

LINQ用時:448毫秒,篩選了9898832條數(shù)據(jù)。
普通用時:437毫秒,篩選了9898832條數(shù)據(jù)。

第2次測試,測試:10000000條數(shù)據(jù)。

LINQ用時:516毫秒,篩選了9899569條數(shù)據(jù)。
普通用時:460毫秒,篩選了9899569條數(shù)據(jù)。

第3次測試,測試:10000000條數(shù)據(jù)。

LINQ用時:608毫秒,篩選了9899231條數(shù)據(jù)。
普通用時:470毫秒,篩選了9899231條數(shù)據(jù)。

結論:LINQ在實現(xiàn)靈活性提高編寫效率的時候犧牲了一定的性能,當然這個是必須的,有的必有失嘛。

我的選擇:絕大部分時候使用Linq,在對性能要求高的時候使用普通的迭代;

 0毫秒的秘密:

  1. var linqList = (from num in list1 where num > 100 select num) 

先看看這個LinqList的類型(Console.WriteLine(linqList.GetType().FullName);):System.Linq.Enumerable+WhereListIterator`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

可以看到這是一個嵌套類,作用是對Where 條件進行迭代操作,貼上它的源代碼:

  1. ?class WhereListIterator<TSource> : Iterator<TSource>  {      
  2.  List<TSource> source;       
  3. Func<TSource, bool> predicate;     List<TSource>.Enumerator enumerator;       
  4.   public WhereListIterator(List<TSource> source, Func<TSource, bool> predicate) {            
  5. this.source = source;           
  6.  this.predicate = predicate;      
  7.  }        
  8.  public override Iterator<TSource> Clone() {         return new WhereListIterator<TSource>(source, predicate);      
  9.  }       
  10.   public override bool MoveNext() {          switch (state) {              case 1:                 enumerator = source.GetEnumerator();                  state = 2;             
  11.       goto case 2;        
  12.        case 2:             
  13.       while (enumerator.MoveNext()) {                     
  14.    TSource item = enumerator.Current;                    
  15.    if (predicate(item)) {                          current = item;                      
  16.       return true;      
  17.                  }           
  18.          }               
  19.     Dispose();            
  20.        break;         }        
  21.     return false;     }   
  22.       public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector) {          
  23.  return new WhereSelectListIterator<TSource, TResult>(source, predicate, selector);      }    
  24.     public override IEnumerable<TSource> Where(Func<TSource, bool> predicate) {       
  25.     return new WhereListIterator<TSource>(source, CombinePredicates(this.predicate, predicate));     
  26.    } }  

真相大白于天下。   

ps:下面是原文代碼的截圖

原文鏈接:http://www.cnblogs.com/jinzhao/archive/2012/05/08/2490543.html

【編輯推薦】
 

  1. Linq to xml操作XML
  2. XML之父解讀未來互聯(lián)網(wǎng)"游戲化"的三個真諦
  3. Ajax和Web服務數(shù)據(jù)格式:XML SOAP HTML
  4. 超強解析XML——簡單直接的來
  5. 解析PHP中的XML數(shù)據(jù) 

 

責任編輯:彭凡 來源: 博客園
相關推薦

2012-05-09 09:55:17

LINQ

2021-05-18 12:35:00

Divdom產(chǎn)品經(jīng)理

2021-10-12 00:04:24

腳本備份MariDB

2019-11-21 09:25:23

AI 數(shù)據(jù)人工智能

2010-04-13 10:02:16

索引

2016-05-19 17:10:27

銀行

2022-10-20 08:00:37

機器人ZadigChatOps

2020-10-16 18:33:18

Rust語言前端開發(fā)

2021-02-26 21:25:08

比特幣投資貨幣

2013-04-19 10:42:02

打車軟件大數(shù)據(jù)

2025-04-22 08:00:00

2013-05-06 15:42:49

2012-06-13 11:01:59

英特爾

2023-04-06 06:55:24

ChatGPTGPT算力

2022-04-24 11:52:04

元宇宙Web3去中心化

2021-03-03 21:24:57

數(shù)據(jù)倉庫工具

2010-06-09 16:57:14

路由選擇協(xié)議

2009-09-07 14:39:14

2011-05-18 11:31:56

數(shù)據(jù)安全數(shù)據(jù)備份

2011-07-01 09:31:49

.net
點贊
收藏

51CTO技術棧公眾號