日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
對(duì)比List<T>搜索和排序中不同方法的性能

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

#T#

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

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

 
 
  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方法搜索用時(shí)" + sw.ElapsedMilliseconds.ToString() + "
    "); 

2:傳統(tǒng)的for循環(huán)。 

 
 
  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循環(huán)搜索用時(shí)" + 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搜索用時(shí)" + sw.ElapsedMilliseconds.ToString() + "
    "); 

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

Find方法搜索用時(shí)5

for循環(huán)搜索用時(shí)4

linq搜索用時(shí)6

第二:再來(lái)看對(duì)List的排序,這里比較List提供的Sort方法和Linq方式的orderby。

1:Sort。這里先寫(xiě)一個(gè)自定義的比較類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排序用時(shí)" + 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排序用時(shí)" + sw.ElapsedMilliseconds.ToString() + "
    "); 

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

Sort排序用時(shí)670

linq排序用時(shí)195

總結(jié)

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

未解問(wèn)題:

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


分享標(biāo)題:對(duì)比List<T>搜索和排序中不同方法的性能
網(wǎng)站鏈接:http://m.5511xx.com/article/cogojod.html