最近在对比List下的两大功臣,功臣之一为ArrayList;功臣之二为LinkedList;
通过这次再次深入理解,有了如下的几点重新价值观刷新:

  • 【插入】我们都知道因为ArrayList底层是数组实现的;LinkedList底层是链表实现的;所以对于新增数据而言,肯定是LinkedList性能远远高于ArrayList的性能;但实践结果并非如此,ArrayList的新增性能并不低,而且通过测试,其比LinkedList插入速度还快。(测试JDK版本 1.8)
  • 【获取】由于LinkedList是链表结构,没有角标的概念,没有实现RandomAccess接口,不具备随机元素访问功能,所以在get方面表现的差强人意,ArrayList再一次完胜。具体为何LinkedList会这么慢呢?其实是因为LinkedList是链表结构,没有角标的概念,没有实现RandomAccess接口,不具备随机元素访问功能,所以在get方面表现的差强人意
  • 【随机根据某个索引插入元素,到底谁快呢?】
    操作代码如下:
//迭代次数public static int ITERATION_NUM = 10000000;public static void main(String[] agrs) {insertPerformanceCompare();}//新增性能比较:public static void insertPerformanceCompare() {System.out.println("LinkedList新增测试开始");long start = System.nanoTime();List<Integer> linkedList = new LinkedList<Integer>();for (int x = 0; x < ITERATION_NUM; x++) {linkedList.add(x);}long end = System.nanoTime();System.out.println("linkedList time one:"+(end - start));long startInsert=System.nanoTime();//依次执行,看执行时间linkedList.add(0,11);linkedList.add(10,11);linkedList.add(100,11);linkedList.add(1000,11);linkedList.add(10000,11);linkedList.add(100000,11);linkedList.add(1000000,11);long endInsert=System.nanoTime();System.out.println("linkedList time two:"+(endInsert-startInsert));System.out.println("ArrayList新增测试开始");long startArray = System.nanoTime();List<Integer> arrayList = new ArrayList<Integer>();for (int x = 0; x < ITERATION_NUM; x++) {arrayList.add(x);}long endArray = System.nanoTime();System.out.println("arrayList time one:"+(endArray - startArray));long startArrayInsert=System.nanoTime();arrayList.add(0,11);arrayList.add(10,11);arrayList.add(100,11);arrayList.add(1000,11);arrayList.add(10000,11);arrayList.add(100000,11);arrayList.add(1000000,11);long endArrayInsert=System.nanoTime();System.out.println("arrayList time two:"+(endArrayInsert-startArrayInsert));}
index insert ArrayList LinkedList
0 3646828 33657
10 3670657 33118
100 3628176 33256
1000 3270814 44075
10000 3696530 128725
100000 3656730 1010909
1000000 3606553 10099185
  • 【随机删除某一个位置的元素,到底谁快呢?】

remove关键代码

      linkedList.remove(0);linkedList.remove(10);linkedList.remove(100);linkedList.remove(1000);linkedList.remove(10000);linkedList.remove(100000);linkedList.remove(1000000);arrayList.remove(0);arrayList.remove(10);arrayList.remove(100);arrayList.remove(1000);arrayList.remove(10000);arrayList.remove(100000);arrayList.remove(1000000);
index del ArrayList LinkedList
0 3319289 16816
10 3796653 17964
100 3462862 19898
1000 3474424 27497
10000 3320296 113838
100000 3312930 1803475
1000000 3029520 10050950

当看到如上数据时,不知道第一感是作何感受?其实当理解了其底层实现的时候,就不吃惊了。
通过如上数据可以发现,对于ArrayList而言,其不管是通过索引增加到哪个位置还是移除某个元素,其用的时间都是均等的;而对于LinkedList而言,则随着索引的增加用的时间逐渐增大。所以以上得到结论:

  • void add(int index, E element) 当数据量超过6位数之后,则考虑使用ArrayList吧
  • E remove(int index) 当数据量超过6位数之后,则考虑使用ArrayList吧

为何会出现这么大的差距呢?上述文章中我们曾提到过,对于ArrayList而言,因为其实现了RandomAccess,有了快速随机访问存储元素的功能,所以只要是通过索引的操作,其都是通过角标实现的,进而不用遍历整个集合,所以效率更高。

转载于:https://www.cnblogs.com/huohuoL/p/10545414.html

ArrayListLinkedList 性能比较相关推荐

  1. kali2020进入单模式_蚂蚁集团技术专家山丘:性能优化的常见模式及趋势

    陈显铭(山丘) 读完需要 6分钟 速读仅需 2 分钟 陈显铭,花名山丘,就职于蚂蚁集团,对分布式应用架构.服务化.性能优化等有深入的理解.参与支付宝支付链路核心系统,设计.调优应用系统关键能力, 高效 ...

  2. Go 学习笔记(81)— Go 性能分析工具 pprof

    Go 语言工具链中的 go pprof 可以帮助开发者快速分析及定位各种性能问题,如 CPU消耗 .内存分配及阻塞分析 .具体作用如下: 性能分析首先需要使用 runtime.pprof 包嵌入到待分 ...

  3. pyspark性能调优参数

    20220311 参数调节 把executor数量调小,其他参数值调大,不容易报错 一.指定spark executor 数量的公式 executor 数量 = spark.cores.max/spa ...

  4. 矩阵乘法的性能提升 AutoKernel

    随着AI技术的快速发展,深度学习在各个领域得到了广泛应用.深度学习模型能否成功在终端落地应用,满足产品需求,一个关键的指标就是神经网络模型的推理性能.于是,一大波算法工程师为了算法的部署转岗算子优化工 ...

  5. TensorFlow与PyTorch模型部署性能比较

    TensorFlow与PyTorch模型部署性能比较 前言 2022了,选 PyTorch 还是 TensorFlow?之前有一种说法:TensorFlow 适合业界,PyTorch 适合学界.这种说 ...

  6. App性能分析数据监控

    App性能分析数据监控 APP的性能监控包括: CPU 占用率.内存使用情况.网络状况监控.启动时闪退.卡顿.FPS.使用时崩溃.耗电量监控.流量监控等等. 文中所有代码都已同步到github中,有兴 ...

  7. Yolo v4, v3 and v2 性能极简图示

    Yolo v4, v3 and v2 性能图示 https://github.com/AlexeyAB/darknet 参考链接: https://github.com/AlexeyAB/darkne ...

  8. Arm Cortex-M4 MCU性能

    Arm Cortex-M4 MCU性能 基于ARM Cortex-M和RISC-V内核,提供了丰富的产品组合和全面的软硬件支持. Arm Cortex-M4 MCU 基于Arm® Cortex®-M4 ...

  9. Arm Cortex-M3 MCU性能

    Arm Cortex-M3 MCU性能 基于ARM Cortex-M和RISC-V内核,研发出来了产品组合和全面的软硬件支持. Arm Cortex-M3 MCU 基于Arm® Cortex®-M3内 ...

最新文章

  1. 常用 Git 命令清单
  2. 老王Python-进阶篇4-异常处理1.3(周末习题)
  3. SprintBoot开发官方指导文档
  4. 计算机视觉与深度学习 | SLAM综述(自主移动机器人同时定位与地图创建)
  5. 用MATLAB三步完成机器人搭建
  6. coding ssh端口指定_443 端口的 SSH 服务
  7. Redis链表结构深入
  8. 5招详解linux之openEuler /centos7防火墙基本使用指南
  9. AbstractSyntax Tree (AST)
  10. iOS 设计模式之抽象工厂
  11. Javascript 立即执行函数
  12. matlab 警告(warning)、错误(error)、异常(exception)与断言(assert)
  13. SAS Planet下载卫星地图
  14. 学术界布局区块链,日本东京大学开始提供区块链课程
  15. linux下kegg注释软件,如何使用KAAS进行KEGG注释
  16. 【必应】Bing自动提交收录python脚本
  17. precede和previous_preceding,previous,prior辨析.ppt
  18. Chango的数学Shader世界(十四)细线间断,发光闪烁,TAA削弱处理
  19. 单片机控制秒表C语言程序,89C51单片机秒表的设计(全文完整版)
  20. Houdini 节点

热门文章

  1. 84. ExtJS下页面显示中文乱码问题
  2. office2003/2007/2010如何卸载干净
  3. ASCX呼叫ASPX.CS的方法
  4. weblogic 的一些说明
  5. 安装Exchange2003时出0XC1037AE6错误的解决方法.
  6. 变态题大串烧:微软面试问题 -- 三.难题:这类题有一定难度,如果得不到答案,也不能说明什么...
  7. python下处理win和linux分行符
  8. Java实现算法导论中KMP字符串匹配算法
  9. RocketMQ--生产者与消费者的简单示例
  10. Spring的新注解——Configuration、ComponentScan、Bean、Import、PropertySource || spring整合Junit分析