Linkedlist与ArrayList的各种操作性能对比-单线程对比

为了增加说服力,每一种操作都可以设置重复次数,重复次数越大,求平均的结果越有说服力。这里根据情况设置了1或者10(见main函数的注释)。有时间同学可以设大一些。

先贴结果:

分别对比的是:add(); addlast(); get(); remove() 方法。

insert into arraylist for 10 times cost:112
insert into linkedlist for 10 times cost:128

insert into the last of arraylist for 10 times cost:32
insert into the last of linkedlist for 10 times cost:33

find from arraylist for 1 times cost:6
find from linkedlist for 1 times cost:5261

remove 1000 numbers from arraylist for 1 times cost:70
remove 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);
}

}









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章