关键词搜索

源码搜索 ×
×

C#中List集合的排序方法总结

发布2018-06-01浏览11551次

详情内容

C#中List集合的排序方法有Where ,AsParallel().Where,GroupBy和ToLookup。

控制台示例程序:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MyList
  8. {
  9. class Program
  10. {
  11. public class Custom
  12. {
  13. public string Name { get; set; }
  14. public int Age { get; set; }
  15. public string Address { get; set; }
  16. }
  17. /// <summary>
  18. /// AsParallel排序测试效率:并发效率优于普通
  19. /// <mark>
  20. /// 测试现象:并行排序的效率低因为并行是乱序的,虽说并发的理论上要比直接排序块,但中间List 又多了一步操作AsParallel,这个地方应该会耗时,并发是乱序排序就跟List差不多了,加上AsParallel操作就会更慢。
  21. /// </mark>
  22. /// </summary>
  23. public static void AsParallel()
  24. {
  25. Stopwatch sw = new Stopwatch();
  26. List<Custom> customs = new List<Custom>();
  27. for (int i = 0; i < 2000000; i++)
  28. {
  29. customs.Add(new Custom() { Name = "Jack", Age = 21, Address = "NewYork" });
  30. customs.Add(new Custom() { Name = "Jime", Age = 26, Address = "China" });
  31. customs.Add(new Custom() { Name = "Tina", Age = 29, Address = "ShangHai" });
  32. customs.Add(new Custom() { Name = "Luo", Age = 30, Address = "Beijing" });
  33. customs.Add(new Custom() { Name = "Wang", Age = 60, Address = "Guangdong" });
  34. customs.Add(new Custom() { Name = "Feng", Age = 25, Address = "YunNan" });
  35. }
  36. sw.Restart();
  37. sw.Start();
  38. var result = customs.Where<Custom>(c => c.Age > 26).ToList();
  39. sw.Stop();
  40. Console.WriteLine("Linq time is {0}.", sw.ElapsedMilliseconds);
  41. sw.Restart();
  42. sw.Start();
  43. var result2 = customs.AsParallel().Where<Custom>(c => c.Age > 26).ToList();
  44. sw.Stop();
  45. Console.WriteLine("Parallel Linq time is {0}.", sw.ElapsedMilliseconds);
  46. }
  47. /// <summary>
  48. /// ToLookup排序:ToLookup方法是将集合转换成一个只读集合,所以在大数据量分组时性能优于List
  49. /// <mark>
  50. /// 测试现象:虽然是只读,但是中间转换过程耗时,所以看到的现象比普通的更慢.
  51. /// </mark>
  52. /// </summary>
  53. public static void ToLookup()
  54. {
  55. Stopwatch stopWatch = new Stopwatch();
  56. List<Custom> customs = new List<Custom>();
  57. for (int i = 0; i < 2000000; i++)
  58. {
  59. customs.Add(new Custom() { Name = "Jack", Age = 21, Address = "NewYork" });
  60. customs.Add(new Custom() { Name = "Jime", Age = 26, Address = "China" });
  61. customs.Add(new Custom() { Name = "Tina", Age = 29, Address = "ShangHai" });
  62. customs.Add(new Custom() { Name = "Luo", Age = 30, Address = "Beijing" });
  63. customs.Add(new Custom() { Name = "Wang", Age = 60, Address = "Guangdong" });
  64. customs.Add(new Custom() { Name = "Feng", Age = 25, Address = "YunNan" });
  65. }
  66. stopWatch.Restart();
  67. var groupByAge = customs.GroupBy(item => item.Age).ToList();
  68. foreach (var item in groupByAge)
  69. {
  70. Console.WriteLine("Age={0},count = {1}", item.Key, item.Count());
  71. }
  72. stopWatch.Stop();
  73. Console.WriteLine("Linq group by time is: " + stopWatch.ElapsedMilliseconds);
  74. stopWatch.Restart();
  75. var lookupList = customs.ToLookup(i => i.Age);
  76. foreach (var item in lookupList)
  77. {
  78. Console.WriteLine("LookUP:Age={0},count = {1}", item.Key, item.Count());
  79. }
  80. stopWatch.Stop();
  81. Console.WriteLine("LookUp group by time is: " + stopWatch.ElapsedMilliseconds);
  82. }
  83. /// <summary>
  84. /// 函数入口
  85. /// </summary>
  86. /// <param name="args"></param>
  87. static void Main(string[] args)
  88. {
  89. Console.WriteLine("AsParallel is runing");
  90. for (var i = 0; i < 10; i++)
  91. {
  92. AsParallel();
  93. }
  94. Console.WriteLine("ToLookup is runing");
  95. ToLookup();
  96. Console.Read();
  97. }
  98. }
  99. }
参考地址: http://www.cnblogs.com/yunfeifei/p/3998783.html

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载