ArrayList与List性能测试

理论:由于ArrayList存储数据存在装箱(读取数据存在拆箱),而泛型List<T>直接对T类型数据进行存储,不存在装箱与拆箱拆箱操作,理论上速度应该快一些。

废话少说,上代码。

 1    public void Test2()
 2         {
 3             float testData = 0.01f;
 4             long length = 100000000;
 5             Stopwatch sw = new Stopwatch();
 6             sw.Start();
 7             ArrayList arrayList = new ArrayList();
 8             for (long i = 0; i < length; i++)
 9             {
10                 arrayList.Add(testData);
11             }
12             sw.Stop();
13 
14             Stopwatch sw2 = new Stopwatch();
15             sw2.Start();
16             List<float> list = new List<float>();
17             for (int i = 0; i < length; i++)
18             {
19                 list.Add(testData);
20             }
21             sw2.Stop();
22             Console.WriteLine("{0}次ArrayList装箱时间{1}", length, sw.Elapsed);
23             Console.WriteLine("{0}次List装箱时间     {1}", length, sw2.Elapsed);
24 
25         }

输出结果ArrayList进行1亿此装箱操作耗时9秒多,而List<T>泛型直接存储数据不到1秒,性能高下立见。

猜你喜欢

转载自www.cnblogs.com/blackteeth/p/10163771.html