ome/img/nav3_on.png">
APP
系统平台
  • 建站知识
  • 联系我们
  • 咨询热线 :
    028-86922220

    疆括仕网站建设,新征程启航

    为企业提供网站建设、域名注册、服务器等服务

    对比List<T>搜索和排序中不同方法的性能

    这里我们对LINQ中LIST搜索和排序,在不同的方法下究竟性能有多少差距。希望通过这样的对比,能让大家在LINQ的应用中能找到最合适的方法。

    #T#

    ***:在.NET 1.1时,我还有很多和我一样的程序员,都会常用到ArrayList,当时要想对这种集合元素进行查找,大多会采用for循环来完成,当然也可以采用BinarySearch 方法。但自从有了.NET 2.0以及.NET 3.5后,ArrayList就已经很少使用了,大家都认为List在性能上要优越于ArrayList。既然有了List,有了LINQ,对于LIST集合的查询就不再单一。我这里列举三种方法:它们共同完成一件事,在一个Person的集合中,查找编号大于50000的元素。Person类定义如下:

     
     
    1. public class Person  
    2. {  
    3.     public string firstName  
    4.     { get; set; }  
    5.     public string lastName  
    6.     { get; set; }  
    7.     public int ID  
    8.     { get; set; }  

    先构造一个Person的泛型集合。当然一般情况下不会有这样的大集合,但为了比较不同方法的搜索性能,这是有必要的。

     
     
    1. List list = new List();  
    2. for (int i = 0; i < 100001; i++)  
    3. {  
    4.     Person p = new Person();  
    5.     p.firstName = i.ToString() + "firstName";  
    6.     p.lastName = i.ToString() + "lastName";  
    7.     list.Add(p);  
    8.  

    1:List提供的FindAll方式。  

     
     
    1. public class FindPerson  
    2. {  
    3.     public string firstName;  
    4.     public FindPerson(string _firstName)  
    5.     { this.firstName = _firstName; }  
    6.     public bool PersonPredicate(Person p)  
    7.     {  
    8.         return p.ID >= 50000;  
    9.     }  
    10. }  
    11. Stopwatch sw = new Stopwatch();  
    12. sw.Start();  
    13. List persons = list.FindAll(new Predicate(fp.PersonPredicate));  
    14. sw.Stop();  
    15. Response.Write("Find方法搜索用时" + sw.ElapsedMilliseconds.ToString() + "
      "); 

    2:传统的for循环。 

     
     
    1. sw.Start();  
    2. List newPersons = new List();  
    3. for (int j = 0; j < list.Count; j++)  
    4. {  
    5.     if (list[j].ID  >= 50000)  
    6.     {  
    7.         newPersons.Add(list[j]);  
    8.  
    9.     }  
    10. }  
    11. sw.Stop();  
    12. Response.Write("for循环搜索用时" + sw.ElapsedMilliseconds.ToString() + "
      "); 

    3:LINQ方式查询。 

     
     
    1. sw = new Stopwatch();  
    2. sw.Start();  
    3. var pn = (from m in list  
    4.           where m.ID >=50000  
    5.           select m).ToList ();  
    6. sw.Stop();  
    7. Response.Write("linq搜索用时" + sw.ElapsedMilliseconds.ToString() + "
      "); 

    输出结果:虽然用时差不多,但还是传统的for循环性能***,尽管写法上并无新意。FindAll我觉的有一点比较好的就是,如果针对List有很多种查询方式,(当然实际情况中Person类不会这么简单),把查询方式封闭在FindPerson类中比较好,这样在外部调用查询时会非常简单。如果是其它的方式,也可以封装,但明显在代码结构上要稍差。Linq方式的查询,在灵活性上我觉的比起前两种要差一些。

    Find方法搜索用时5

    for循环搜索用时4

    linq搜索用时6

    第二:再来看对List的排序,这里比较List提供的Sort方法和Linq方式的orderby。

    1:Sort。这里先写一个自定义的比较类PersonComparer

     
     
    1. public class PersonComparer : IComparer  
    2. {  
    3.     public int Compare(Person x, Person y)  
    4.     {  
    5.         return x.ID.CompareTo(y.ID);  
    6.     }  
    7.  

    排序代码:

     
     
    1. sw = new Stopwatch();  
    2. sw.Start();  
    3. list.Sort(new PersonComparer());  
    4. sw.Stop();  
    5. Response.Write("Sort排序用时" + sw.ElapsedMilliseconds.ToString() + "
      "); 

    2:Linq方式。

     
     
    1. sw = new Stopwatch();  
    2. sw.Start();  
    3. var pn = (from m in list  
    4.          orderby m.ID descending  
    5.           select m).ToList();  
    6. sw.Stop();  
    7. Response.Write("linq排序用时" + sw.ElapsedMilliseconds.ToString() + "
      "); 

    输出结果:在排序上linq还是占有比较大的优势。

    Sort排序用时670

    linq排序用时195

    总结

    对于泛型集合的操作,并不能一味的说某种方式有绝对的优势,需要根据对应的情景来选择不同的处理方式。有时候最常见的最简单的也许是性能***的。新技术当然有它的优点, Linq提供的排序在性能上就有明显的优势,但在查询方面也是最差的,尽管差距不大。

    未解问题:

    至于为什么for循环在查询时性能***,LINQ在排序上为什么性能好,本人并不知道其中原因,如有知道的朋友,请指教。


    网页标题:对比List<T>搜索和排序中不同方法的性能
    当前地址:https://www.tyhkzb.com/article/cogojod.html
    在线咨询
    服务热线
    服务热线:028-86922220
    TOP