zoukankan      html  css  js  c++  java
  • 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秒,性能高下立见。

  • 相关阅读:
    leetcode--91--递归与动态规划
    Grunt体验
    JSON标准中引号是双引号,不是单引号!
    npm install
    正则-手机号隐藏中间四位及tips备忘
    node tips
    技术栈
    slice与remove
    substr、substring、slice
    springboot 取消post数据大小限制
  • 原文地址:https://www.cnblogs.com/blackteeth/p/10163771.html
Copyright © 2011-2022 走看看