Linkedlist与ArrayList的各种操作性能对比-单线程对比
来源:互联网 发布:highlight.js 行号 编辑:程序博客网 时间:2024/05/19 01:32
为了增加说服力,每一种操作都可以设置重复次数,重复次数越大,求平均的结果越有说服力。这里根据情况设置了1或者10(见main函数的注释)。有时间同学可以设大一些。
先贴结果:
分别对比的是:add(); addlast(); get(); remove() 方法。
insert into arraylist for 10 times cost:112insert into linkedlist for 10 times cost:128
insert into the last of arraylist for 10 times cost:32insert into the last of linkedlist for 10 times cost:33
find from arraylist for 1 times cost:6find from linkedlist for 1 times cost:5261
remove 1000 numbers from arraylist for 1 times cost:70remove 1000 numbers from linkedlist for 1 times cost:1
结果分析:
add方法:对比性能较为明显:linkedlist的插入时间明显长一些。
addlast方法:区别不大:从尾部插入至linkedlist时间可能还要短一些(原因有待分析)。
get方法:linkedlist 性能差很多。
remove方法:linkedlist明显优于arraylist。
上一篇从源代码上做了二者简单的分析。这一篇编写单线程代码对比二者性能。
贴代码:
import java.util.ArrayList;import java.util.LinkedList;/** * Created by aaashen on 2015/4/8. */public class Vs { private ArrayList alist = new ArrayList<Integer>(); private LinkedList llist = new LinkedList<Integer>(); private long startTime,startTime2 = 0; private long endTime,endTime2 = 0; private long costTime,totalCostTime = 0;/** @param times* 求平均的次数* @param num* 向list中操作的次数* */ private void testArrayInsert(int times, int num) { long totalCostTime = 0; long costTime = 0; long startTime = 0; long endTime = 0; for(int j=0; j<times; j++) { alist = null; alist = new ArrayList<Integer>(); costTime = 0; startTime = 0; endTime = 0; startTime = System.currentTimeMillis(); for (int i = 0; i < num; i++) { alist.add(i); } endTime = System.currentTimeMillis(); costTime = endTime - startTime; totalCostTime += costTime; } System.out.println("insert into arraylist for "+times+" times cost:" + totalCostTime/times); } private void testLinkedInsert(int times, int num){ long totalCostTime = 0; long costTime = 0; long startTime = 0; long endTime = 0; for(int j=0; j<times; j++) { llist = null; llist = new LinkedList<Integer>(); costTime = 0; startTime = 0; endTime = 0; startTime = System.currentTimeMillis(); for (int i = 0; i < num; i++) { llist.add(i); } endTime = System.currentTimeMillis(); costTime = endTime - startTime; totalCostTime += costTime; } System.out.println("insert into linkedlist for "+times+" times cost:" + totalCostTime/times); } private void testArrayInsertLast(int times, int num) { long totalCostTime = 0; long costTime = 0; long startTime = 0; long endTime = 0; for(int j=0; j<times; j++) { alist = null; alist = new ArrayList<Integer>(); costTime = 0; startTime = 0; endTime = 0; startTime = System.currentTimeMillis(); for (int i = 0; i < num; i++) { alist.add(i); } endTime = System.currentTimeMillis(); costTime = endTime - startTime; totalCostTime += costTime; } System.out.println("insert into the last of arraylist for "+times+" times cost:" + totalCostTime/times); } private void testLinkedInsertLast(int times, int num){ long totalCostTime = 0; long costTime = 0; long startTime = 0; long endTime = 0; for(int j=0; j<times; j++) { llist = null; llist = new LinkedList<Integer>(); costTime = 0; startTime = 0; endTime = 0; startTime = System.currentTimeMillis(); for (int i = 0; i < num; i++) { llist.addLast(i); } endTime = System.currentTimeMillis(); costTime = endTime - startTime; totalCostTime += costTime; } System.out.println("insert into the last of linkedlist for "+times+" times cost:" + totalCostTime/times); } private void testArrayFind(int times, int num) { long totalCostTime = 0; long costTime = 0; long startTime = 0; long endTime = 0; for(int j=0; j<times; j++) { alist = null; alist = new ArrayList<Integer>(); costTime = 0; startTime = 0; endTime = 0; for (int i = 0; i < num; i++) { alist.add(i); } startTime = System.currentTimeMillis(); for (int i = 0; i < num; i++) { alist.get(i); } endTime = System.currentTimeMillis(); costTime = endTime - startTime; totalCostTime += costTime; } System.out.println("find from arraylist for "+times+" times cost:" + totalCostTime/times); } private void testLinkedFind(int times, int num){ long totalCostTime = 0; long costTime = 0; long startTime = 0; long endTime = 0; for(int j=0; j<times; j++) { llist = null; llist = new LinkedList<Integer>(); costTime = 0; startTime = 0; endTime = 0; for (int i = 0; i < num; i++) { llist.add(i); } startTime = System.currentTimeMillis(); for (int i = 0; i < num; i++) { llist.get(i); } endTime = System.currentTimeMillis(); costTime = endTime - startTime; totalCostTime += costTime; } System.out.println("find from linkedlist for "+times+" times cost:" + totalCostTime/times); } /* * @param times * 求平均的次数 * @param num * 向list中操作的次数 * @param num_remove * 删除的次数 * */ private void testArrayRemove(int times, int num, int num_remove) { long totalCostTime = 0; long costTime = 0; long startTime = 0; long endTime = 0; for(int j=0; j<times; j++) { alist = null; alist = new ArrayList<Integer>(); costTime = 0; startTime = 0; endTime = 0; for (int i = 0; i < num; i++) { alist.add(i); } startTime = System.currentTimeMillis(); for (int i = 0; i < num_remove; i++) { alist.remove(i); } endTime = System.currentTimeMillis(); costTime = endTime - startTime; totalCostTime += costTime; } System.out.println("remove "+num_remove+" numbers from arraylist for "+times+" times cost:" + totalCostTime/times); } private void testLinkedRemove(int times, int num, int num_remove){ long totalCostTime = 0; long costTime = 0; long startTime = 0; long endTime = 0; for(int j=0; j<times; j++) { llist = null; llist = new LinkedList<Integer>(); costTime = 0; startTime = 0; endTime = 0; for (int i = 0; i < num; i++) { llist.add(i); } startTime = System.currentTimeMillis(); for (int i = 0; i < num_remove; i++) { llist.remove(i); } endTime = System.currentTimeMillis(); costTime = endTime - startTime; totalCostTime += costTime; } System.out.println("remove " + num_remove + " numbers from linkedlist for "+times+" times cost:" + totalCostTime/times); } public static void main(String args[]){ Vs vs = new Vs(); //由于不同操作时间长度不同,为了减少等待时间,为同一个参数设置了大小。 int times_small = 1; //重复次数-小 int times_large = 10;//重复次数-大 int num_small = 100000;//操作次数-小 int num_large = 1000000;//操作次数-大 //插入操作时间较短,因此设置的都是大的参数,比较有代表性 vs.testArrayInsert(times_large,num_large); vs.testLinkedInsert(times_large,num_large); vs.testArrayInsertLast(times_large, num_large); vs.testLinkedInsertLast(times_large, num_large); //linkedlist删除操作花太久时间,因此传入了小的参数,不影响比较 vs.testArrayFind(times_small, num_small); vs.testLinkedFind(times_small, num_small); //删除操作直接设置为1000次删除 vs.testArrayRemove(times_small, num_small, 1000); vs.testLinkedRemove(times_small, num_small, 1000);}}
1 0
- Linkedlist与ArrayList的各种操作性能对比-单线程对比
- ArrayList和LinkedList的操作性能对比
- ArrayList和LinkedList的操作性能对比
- ArrayList和LinkedList的操作性能对比
- 关于ArrayList与LinkedList插入元素性能的对比
- ArrayList与LinkedList对比
- ArrayList,LinkedList的对比
- ArrayList和LinkedList的对比
- ArrayList和LinkedList性能对比测试
- Java多线程与单线程性能对比
- ArrayList与LinkedList的普通for循环遍历效率对比
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayLis
- ArrayList LinkedList Vector 对比
- ArrayList、LinkedList、Vector 对比
- LinkedList、ArrayList、Vector对比
- ArrayList和LinkedList对比
- ArrayList,LinkedList,Vestor对比
- Java的ArrayList和LinkedList的对比
- 浅谈迭代算法
- .NET中的加解密和数字签名
- 浅析JEECG_V3.5开源框架
- 螺旋矩阵的处理
- 重建二叉树
- Linkedlist与ArrayList的各种操作性能对比-单线程对比
- 浅谈Windows内存管理以及链表使用
- java泛型介绍及使用
- java的基本数据类型与引用
- Java基本知识(一)域 静态域 实例域
- 归并排序
- 老婆的出轨
- 第39级台阶
- hive(一)--hql摘要