出于好奇,我想测试将GenericList与ArrayList进行比较的刻度数.
对于下面的代码,当我检查秒表时,ArrayList似乎更快.
我做错了还是有解释? (我相信List会更快)
private static void ArrayListVsGenericList() { // Measure for ArrayList Stopwatch w0 = new Stopwatch(); w0.Start(); ArrayList aList = new ArrayList(); for (int i = 0; i < 1001; i++) { Point p = new Point(); p.X = p.Y = i; aList.Add(p); } foreach (Point point in aList) { int v0 = ((Point) aList[8]).X; //unBoxing } w0.Stop(); // Measure for Generic List<Point> Stopwatch w1 = new Stopwatch(); w1.Start(); List<Point> list = new List<Point>(); for (int i = 0; i < 1001; i++) { Point p = new Point(); p.X = p.Y = i; list.Add(p); } foreach (var point in list) { int v1 = list[8].X; } w1.Stop(); Console.WriteLine("Watch 0 : " + w0.ElapsedTicks); Console.WriteLine("Watch 1 : " + w1.ElapsedTicks); Console.WriteLine("Watch 0 > Watch 1 : " + (w0.ElapsedTicks > w1.ElapsedTicks)); }