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

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

開發(fā) 后端
本文將為大家介紹LINQ中List<T>搜索和排序中不同方法的性能,希望對大家有所幫助。

這里我們對LINQ中LIST<T>搜索和排序,在不同的方法下究竟性能有多少差距。希望通過這樣的對比,能讓大家在LINQ的應(yīng)用中能找到最合適的方法。

#T#

***:在.NET 1.1時,我還有很多和我一樣的程序員,都會常用到ArrayList,當(dāng)時要想對這種集合元素進(jìn)行查找,大多會采用for循環(huán)來完成,當(dāng)然也可以采用BinarySearch 方法。但自從有了.NET 2.0以及.NET 3.5后,ArrayList就已經(jīng)很少使用了,大家都認(rèn)為List<T>在性能上要優(yōu)越于ArrayList。既然有了List<T>,有了LINQ,對于LIST<T>集合的查詢就不再單一。我這里列舉三種方法:它們共同完成一件事,在一個Person的集合中,查找編號大于50000的元素。Person類定義如下:

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

先構(gòu)造一個Person的泛型集合。當(dāng)然一般情況下不會有這樣的大集合,但為了比較不同方法的搜索性能,這是有必要的。

  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.     list.Add(p);  
  8.  

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:傳統(tǒng)的for循環(huán)。   

  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循環(huán)搜索用時" + 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/>"); 

輸出結(jié)果:雖然用時差不多,但還是傳統(tǒng)的for循環(huán)性能***,盡管寫法上并無新意。FindAll我覺的有一點比較好的就是,如果針對List<Person>有很多種查詢方式,(當(dāng)然實際情況中Person類不會這么簡單),把查詢方式封閉在FindPerson類中比較好,這樣在外部調(diào)用查詢時會非常簡單。如果是其它的方式,也可以封裝,但明顯在代碼結(jié)構(gòu)上要稍差。Linq方式的查詢,在靈活性上我覺的比起前兩種要差一些。

Find方法搜索用時5

for循環(huán)搜索用時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.  

排序代碼:

  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/>"); 

輸出結(jié)果:在排序上linq還是占有比較大的優(yōu)勢。

Sort排序用時670

linq排序用時195

總結(jié)

對于泛型集合的操作,并不能一味的說某種方式有絕對的優(yōu)勢,需要根據(jù)對應(yīng)的情景來選擇不同的處理方式。有時候最常見的最簡單的也許是性能***的。新技術(shù)當(dāng)然有它的優(yōu)點, Linq提供的排序在性能上就有明顯的優(yōu)勢,但在查詢方面也是最差的,盡管差距不大。

未解問題:

至于為什么for循環(huán)在查詢時性能***,LINQ在排序上為什么性能好,本人并不知道其中原因,如有知道的朋友,請指教。

原文標(biāo)題:List<T>在搜索和排序時采用不同方法的性能比較

鏈接:http://www.cnblogs.com/ASPNET2008/archive/2010/01/20/1652062.html

責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2009-08-27 13:56:03

IEnumerable

2009-10-28 09:23:27

WF4.0 Beta2

2012-06-08 03:36:30

C#Java

2023-01-03 07:49:45

Java隨機(jī)數(shù)線程

2021-12-25 23:17:52

Windows 11Windows微軟

2020-06-29 07:23:54

for循環(huán)數(shù)組JavaScrip

2010-09-02 10:15:46

SQL刪除

2010-02-23 14:24:50

WCF狀態(tài)保存

2020-04-20 14:30:54

UbuntuLinux內(nèi)核

2020-04-24 16:09:57

UbuntuLinux內(nèi)核

2020-04-21 17:04:12

JavaScriptHTTP請求開發(fā)

2023-05-08 16:12:29

算法k近鄰算法KNN

2021-12-20 07:11:26

Java List排序 Java 基礎(chǔ)

2015-12-14 09:42:34

Java不同排序方法

2009-08-28 17:18:55

foreach循環(huán)

2019-09-26 08:07:06

RHEL8命令Linux

2012-12-13 10:32:34

路由器線路輸出

2010-12-23 13:56:55

SharePointIntranet

2015-07-28 10:43:38

js\height\

2009-08-20 17:30:02

C#連接字符串
點贊
收藏

51CTO技術(shù)棧公眾號