zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

对《LINQ能不能用系列1数组筛选效率对比》的几个问题

2023-03-14 22:46:54 时间

引用原文:LINQ能不能用系列(一)数组筛选效率对比

错误一:作为对比测试,测试数组应该为同一个,否则测试数据没有可比性

错误二:对比组中对List的使用不对,List默认创建的数组大小为4,每次增长为4,那么这里就会反复重新创建新的数组次数为log10000000次左右当然会比Linq慢很多

错误三:面对Linq接近0毫秒的处理能力,稍微有点经验的同学就知道这是不可能的,除非是很强很强的计算机,至于为什么后面给出答案,总之linq查询里肯定有猫腻,直接调用tolist()强制返回结果再说;//这里Stone W在评论中对ToList有质疑,我之所以ToList是为了和第二组进行对比,因为第二组得到的结果是一个List,我很奇怪,这次的对比测试到底是为了测试得到两个结果集的算法对比呢还是测试Count算法的对比呢?如果是前者,一个拿到的是IEnumerable的对象一个是List对象,牛跟闹钟怎么对比哪个跑的快呢?也只有在调用ToList的时候才会真正执行Linq的算法也就是下面的嵌套类WhereListIterator;当然如果是为了进行Count对比的话那么对比组二中的算法真的有点拙劣,我想不会有谁会用方法二来统计。

下面是修改了如上三个明显错误后的代码,如果哪位同学有补充欢迎留言:

  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; // 数组长度  
  12.     Random random = new Random(); // 数据随机构建值  
  13.  
  14.     // 数组构建   
  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.     // 效率测试内容:提取数组中数值大于的100的数组  
  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}条数据。    LINQ用时:{1}毫秒,筛选了{2}条数据。  普通用时:{3}毫秒,筛选了{4}条数据。 ",  
  46.                       time, linq_Stopwatch.ElapsedMilliseconds, linqList.Count(),  
  47.                       before_Stopwatch.ElapsedMilliseconds, beforeList.Count(), listCount));  
  48.  

测试结果:

第1次测试,测试:10000000条数据。

LINQ用时:448毫秒,筛选了9898832条数据。
普通用时:437毫秒,筛选了9898832条数据。

第2次测试,测试:10000000条数据。

LINQ用时:516毫秒,筛选了9899569条数据。
普通用时:460毫秒,筛选了9899569条数据。

第3次测试,测试:10000000条数据。

LINQ用时:608毫秒,筛选了9899231条数据。
普通用时:470毫秒,筛选了9899231条数据。

结论:LINQ在实现灵活性提高编写效率的时候牺牲了一定的性能,当然这个是必须的,有的必有失嘛。

我的选择:绝大部分时候使用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之父解读未来互联网"游戏化"的三个真谛
  3. Ajax和Web服务数据格式:XML SOAP HTML
  4. 超强解析XML——简单直接的来
  5. 解析PHP中的XML数据