继续测试-查找一定范围内的自定义对象集合
这次换了一个自定义的对象Student。然后根据成绩(Mark字段,取值范围0-99)范围查找符合条件的学生,重新用一个list装起来。
上代码。
 全部代码
全部代码
1 class Student 2 { 3 public int SID { get; set; } 4 public string SName { get; set; } 5 public int Mark { get; set; } 6 7 } 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 Stopwatch sw = new Stopwatch(); 13 Random rand=new Random(); 14 string output = "{0}运行使用了{1}毫秒"; 15 16 List<Student> intlist = new List<Student>(); 17 const int length = 14843545; 18 List<Student> temp=new List<Student>(); 19 20 21 for (int i = 0; i < length; i++) 22 { 23 intlist.Add(new Student 24 { 25 SID=i, 26 SName=i.ToString(), 27 Mark=rand.Next(100) 28 }); 29 } 30 31 int high = rand.Next(100); 32 int low = rand.Next(high); 33 int mark; 34 35 Console.WriteLine("总元素个数{0},查找范围{1}-{2}", length,low, high); 36 37 List<Student> forlist = new List<Student>(); ; 38 forlist.Clear(); 39 forlist.AddRange(intlist); 40 41 List<Student> lambdalist = new List<Student>(); 42 lambdalist.Clear(); 43 lambdalist.AddRange(intlist); 44 45 List<Student> foreachlist = new List<Student>(); 46 foreachlist.Clear(); 47 foreachlist.AddRange(intlist); 48 49 List<Student> linqlist = new List<Student>(); 50 linqlist.Clear(); 51 linqlist.AddRange(intlist); 52 //------------------------------------ 53 // for 54 sw.Reset(); 55 temp.Clear(); 56 sw.Start(); 57 58 for (int i = 0; i < length; i++) 59 { 60 mark = forlist[i].Mark; 61 if (mark <= high && mark >= low) 62 { 63 temp.Add(forlist[i]); 64 } 65 } 66 sw.Stop(); 67 Console.WriteLine("满足条件的元素有{0}个",temp.Count); 68 Console.WriteLine(output, "for循环", sw.ElapsedMilliseconds); 69 //---------------------------------------------------------- 70 //foreach 71 sw.Reset(); 72 temp.Clear(); 73 sw.Start(); 74 75 foreach (Student item in foreachlist) 76 { 77 mark = item.Mark; 78 if (mark <= high && mark >= low) 79 { 80 temp.Add(item); 81 } 82 } 83 sw.Stop(); 84 Console.WriteLine(output, "foreach循环", sw.ElapsedMilliseconds); 85 //------------------------------------------------ 86 //lambda 87 sw.Reset(); 88 temp.Clear(); 89 sw.Start(); 90 temp= lambdalist.Where(x => x.Mark <= high&& x.Mark>=low).ToList(); 91 92 sw.Stop(); 93 Console.WriteLine(output, "Lambda表达式", sw.ElapsedMilliseconds); 94 //---------------------------------------------------- 95 //Linq 96 97 sw.Reset(); 98 temp.Clear(); 99 sw.Start(); 100 temp = (from y in linqlist 101 where y.Mark<= high && y.Mark>=low 102 select y).ToList(); 103 sw.Stop(); 104 Console.WriteLine(output, "Linq查询", sw.ElapsedMilliseconds); 105 //----------------------------------------------------------- 106 Console.Read(); 107 } 108 }
测试结果:
| 范围 | 符合个数 | for | foreach | Lambda | Linq | 
| 29-68 | 5938385 | 542 | 535 | 692 | 715 | 
| 21-70 | 7422882 | 426 | 651 | 972 | 689 | 
| 14-35 | 3265893 | 606 | 380 | 812 | 493 | 
| 24-46 | 3414418 | 274 | 336 | 547 | 556 | 
| 87-92 | 891347 | 199 | 235 | 347 | 340 | 
| 19-29 | 1632824 | 214 | 274 | 407 | 416 | 
| 32-33 | 296897 | 188 | 264 | 363 | 369 | 
| 3-11 | 1335090 | 193 | 234 | 363 | 395 | 
| 4-7 | 593746 | 216 | 279 | 430 | 619 | 
| 0-6 | 1039583 | 173 | 208 | 319 | 323 | 
| 4-33 | 4451722 | 396 | 380 | 860 | 562 | 
| 12-76 | 9647771 | 433 | 380 | 4426 | 674 | 
| 10-75 | 9793927 | 379 | 361 | 3961 | 651 | 
| 57-99 | 6384332 | 306 | 324 | 634 | 640 | 
这次就有点怪异了。在结果的数据量级比较大时,foreach有几次比for还快。lambda与linq谁也没占多少便宜。达到几秒那几次lambda的时间可能刚好碰上GC了,所以不能作为参考。
    Boniz·Lee


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号