zoukankan      html  css  js  c++  java
  • List<T>在搜索和排序时采用不同方法的性能比较

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

          Person类定义如下:

        public class Person
        {
            
    public string firstName
            { 
    getset; }
            
    public string lastName
            { 
    getset; }
            
    public int ID
            { 
    getset; }
        }

          

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

    代码
                List<Person> list = new List<Person>();
                
    for (int i = 0; i < 100001; i++)
                {
                    Person p 
    = new Person();
                    p.firstName 
    = i.ToString() + "firstName";
                    p.lastName 
    = i.ToString() + "lastName";
                    p.ID = i;
                    list.Add(p);
     
                }

           1:List<T>提供的FindAll方式。
        

    代码
        public class FindPerson
        {
            
    public string firstName;
            
    public FindPerson(string _firstName)
            { 
    this.firstName = _firstName; }
            
    public bool PersonPredicate(Person p)
            {
                
    return p.ID >= 50000;
            }
        }
        Stopwatch sw 
    = new Stopwatch();
        sw.Start();
        List
    <Person> persons = list.FindAll(new Predicate<Person>(fp.PersonPredicate));
        sw.Stop();
        Response.Write(
    "Find方法搜索用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");

           2:传统的for循环。
      

    代码
                sw.Start();
                List
    <Person> newPersons = new List<Person>();
                
    for (int j = 0; j < list.Count; j++)
                {
                    
    if (list[j].ID  >= 50000)
                    {
                        newPersons.Add(list[j]);

                    }
                }
                sw.Stop();
                Response.Write(
    "for循环搜索用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");

      

          3:LINQ方式查询。
       

    代码
                sw = new Stopwatch();
                sw.Start();
                var pn 
    = (from m in list
                          
    where m.ID >=50000
                          select m).ToList 
    <Person >();
                sw.Stop();
                Response.Write(
    "linq搜索用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");

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

         Find方法搜索用时5
         for循环搜索用时4
         linq搜索用时6

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

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

        public class PersonComparer : IComparer<Person>
        {
            
    public int Compare(Person x, Person y)
            {
                
    return x.ID.CompareTo(y.ID);
            }
        
        }

        排序代码:
        

    代码
                sw = new Stopwatch();
                sw.Start();
                list.Sort(
    new PersonComparer());
                sw.Stop();
                Response.Write(
    "Sort排序用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");
       

          2:Linq方式。
       

    代码
                sw = new Stopwatch();
                sw.Start();
                var pn 
    = (from m in list
                         orderby m.ID descending
                          select m).ToList
    <Person>();
                sw.Stop();
                Response.Write(
    "linq排序用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");

        

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

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

         未解问题:

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

  • 相关阅读:
    Spring-Task
    bootstrap table分页(前后端两种方式实现)
    jquery file upload示例
    ajax传递list集合
    cogs 2383. [Hnoi2014]世界树 WD
    cogs 36.求和问题
    bolg
    noip2016
    cogs 1619. [HEOI2012]采花 AC
    leetcode[109]Convert Sorted List to Binary Search Tree
  • 原文地址:https://www.cnblogs.com/ASPNET2008/p/1652062.html
Copyright © 2011-2022 走看看