国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LinQ to SQL 及 non-LinQ方式實現Group的Performance對比

2019-11-17 02:44:30
字體:
來源:轉載
供稿:網友

LinQ to SQL 及 non-LinQ方式實現Group的Performance對比

擁有476550數據的一張數據表。使用其中的某個字段分組,然后按該字段進行排序。該需求分別使用LinQ to SQL和non-LinQ的方式實現,然后來看一下performance對比。

LinQ way

from p in context.Part_Partgroup p by p.FunctionGroup into groupedPsorderby groupedPs.Keyselect groupedPs
LinQ way for group

non-LinQ way

var results = new SortedDictionary<long?, IList<Part_Part>>();foreach (var p in context.Part_Part){    IList<Part_Part> groupedValue = null;    if (!results.TryGetValue(p.FunctionGroup, out groupedValue))    {        groupedValue = new List<Part_Part>();        results[p.FunctionGroup] = groupedValue;    }    groupedValue.Add(p);}        
non-LinQ way for group

var results = new SortedDictionary<long?, IList<Part_Part>>();

可以看出,用來排序的字段類型為long?。先看一下執行時間。

LinQ Waynon-LinQ Way
first time1:6.6986.707
second time1:7.4041.426
third time1:7.1271.486
forth time1:6.9521.425

明顯可以看出在這個scenario下,LinQ的performance極低。調整代碼,這次使用類型為string的PartDescription分組。測試,結果如下。

LinQ Waynon-LinQ way
first time>30min8.738
second time>30min4.201
third time>30min4.173
forth time>30min4.176

這個scenario下,non-LinQ way耗時有所增加,而LinQ way更是慘不忍睹。甚至在苦苦的等了30分鐘不見結果后,提早結束了測試程序。

可見,LinQ在帶來簡潔風和極佳可讀性的同時,也帶來了性能的損耗。看一段來自于《LinQ in Action》中,關于LinQ性能問題的描述。

There are no surPRises. LINQ does not come for free. LINQ queries cause additional work, object creations, and pressure on the garbage collector. The additional cost of using LINQ can vary a lot depending on the query. It can be as low as 5 percent, but can sometimes be around 500 percent.

既如此,以后還能不能愉快的使用LinQ呢?再來看一段《LinQ in Action》中的描述。

Do not be afraid to use LINQ, but use it wisely. For simple Operations that are executed extensively in your code, you may consider using the traditional alternatives. For simple filter or search operations, you can stick to the methods offered by List<T> and arrays, such as FindAll, ForEach, Find, ConvertAll, or TrueForAll. Of course, you can continue to use the classic for and foreach statements wherever LINQ would be overkill. For queries that are not executed several times per second, you can probably use LINQ to Objects safely. A query that is executed only once in a non-time-critical context won't make a big difference if it takes 60 milliseconds to execute instead of 10.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贞丰县| 山东省| 光山县| 贵阳市| 梁河县| 会昌县| 石楼县| 贵溪市| 阜阳市| 格尔木市| 泰和县| 廉江市| 安仁县| 北海市| 博乐市| 玛曲县| 昭平县| 二手房| 尼勒克县| 正宁县| 米林县| 弥勒县| 休宁县| 米脂县| 碌曲县| 云林县| 中牟县| 榕江县| 宽城| 阜平县| 扎鲁特旗| 丰顺县| 新宁县| 公安县| 新昌县| 宁蒗| 保亭| 和硕县| 定结县| 桓仁| 芒康县|