(转)List<T>在搜索和排序时采用不同方法的性能比较

本文对比了.NET中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
        { get; set; }
        public string lastName
        { get; set; }
        public int ID
        { get; set; }
    }

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

 1  List<Person> list = new List<Person>();
 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                 p.ID = i;
 8                 list.Add(p);
 9  
10             }

   1:List<T>提供的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<Person> persons = list.FindAll(new Predicate<Person>(fp.PersonPredicate));
14     sw.Stop();
15     Response.Write("Find方法搜索用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");

  2:传统的for循环。

 1 sw.Start();
 2             List<Person> newPersons = new List<Person>();
 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() + "<br/>");

  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 <Person >();
6             sw.Stop();
7             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

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

  排序代码:

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

  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<Person>();
6             sw.Stop();
7             Response.Write("linq排序用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");

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

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

 

 

 

 

 

 

转载于:https://www.cnblogs.com/logicsylar/archive/2013/01/29/2881156.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值