新聞中心
相信LINQ大家已經(jīng)很熟悉了,如果有不太熟的,可以參考MSDN 地址:http://msdn.microsoft.com/zh-cn/library/bb397933.aspx

成都創(chuàng)新互聯(lián)公司秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營銷的理念,以專業(yè)定制企業(yè)官網(wǎng),成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè),微信小程序定制開發(fā),網(wǎng)頁設(shè)計(jì)制作,成都做手機(jī)網(wǎng)站,全網(wǎng)整合營銷推廣幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對客戶都以感恩的心態(tài)奉獻(xiàn)自己的專業(yè)和所長。
緣由:LINQ到底能不能用?究竟好不好用,很多時(shí)候大家還是眾說紛紜,有人迷茫,有人觀望,有人覺得無所謂,或者還有人說只是語法糖,中看不中用,哪我們這個(gè)系列就為大家揭開謎團(tuán)。首先來看Linq在數(shù)組篩選方面的效率測試吧。
實(shí)例分析
測試環(huán)境:Visual Studio 2011 Beta(netframework 4.0+)C# 控制臺程序
測試需求:從10000000條數(shù)據(jù)中查詢大于100的數(shù)據(jù)。
核心代碼(LINQ):
- var linqList = from num in list1
- where num > 100
- select num;
完整代碼(根據(jù)園友意見以調(diào)整):
- ///
- /// 效率測試
- ///
- /// 第幾次測試
- private static void timeTest(int testCount)
- {
- const int listCount = 10000000; // 數(shù)組長度
- Random random = new Random(); // 數(shù)據(jù)隨機(jī)構(gòu)建值
- // 數(shù)組構(gòu)建
- List
listData = new List (); - for (int i = 0; i < listCount; i++)
- {
- listData.Add(random.Next(10000));
- }
- // LINQ 測試
- Stopwatch linq_Stopwatch = new Stopwatch();
- linq_Stopwatch.Start();
- var linqList = from num in listData
- where num > 100
- select num;
- var linqCount = linqList.Count();
- linq_Stopwatch.Stop();
- // 普通方式 測試
- Stopwatch before_Stopwatch = new Stopwatch();
- before_Stopwatch.Start();
- List
beforeList = new List (listCount); - for (int i = 0; i < listData.Count(); i++)
- {
- if (listData[i] > 100)
- beforeList.Add(listData[i]);
- }
- var beforeCount = beforeList.Count;
- before_Stopwatch.Stop();
- // 打印結(jié)果
- Console.WriteLine(String.Format("第{0}次測試,測試:{5}條數(shù)據(jù)。\n\r \t LINQ用時(shí):{1}毫秒,篩選了{(lán)2}條數(shù)據(jù)。\n\r\t 普通用時(shí):{3}毫秒,篩選了{(lán)4}條數(shù)據(jù)。\r\n",
- testCount, linq_Stopwatch.ElapsedMilliseconds, linqCount, before_Stopwatch.ElapsedMilliseconds, beforeCount, listCount));
- }
結(jié)果
結(jié)論:由此可知數(shù)據(jù)篩選,LINQ效率遠(yuǎn)遠(yuǎn)大于之前用的手工篩選。如此LINQ不但語法簡潔優(yōu)雅,而且效率也遠(yuǎn)遠(yuǎn)勝出,所有數(shù)據(jù)篩選LINQ可用。
原文鏈接:http://www.cnblogs.com/stone_w/archive/2012/05/08/2490440.html
【編輯推薦】
- Linq to xml操作XML
- XML之父解讀未來互聯(lián)網(wǎng)"游戲化"的三個(gè)真諦
- Ajax和Web服務(wù)數(shù)據(jù)格式:XML SOAP HTML
- 超強(qiáng)解析XML——簡單直接的來
- 解析PHP中的XML數(shù)據(jù)
網(wǎng)頁名稱:LINQ能不能用系列(一)數(shù)組篩選效率對比
本文路徑:http://m.fisionsoft.com.cn/article/cophohg.html


咨詢
建站咨詢
