测量时间并不能确认LinkedList的优势

时间:2013-10-05 14:32:38

标签: java arraylist linked-list listiterator

我正在阅读When to use LinkedList over ArrayList?中指出的ArrayListLinkedList之间的差异。我开发了一个小的示例applcation来测试LinkedList的一个主要优势,但我得到的结果没有确认,LinkedList在操作性能上超过ArrayList

ListIterator.add(E element)

这是我的代码:

public static void main(String[] args) {

        int number = 100000;

        long startTime1 = System.currentTimeMillis();
        fillLinkedList(number);
        long stopTime1 = System.currentTimeMillis();

        long startTime2 = System.currentTimeMillis();
        fillArrayList(number);
        long stopTime2 = System.currentTimeMillis();

        System.out.println(" LinkedList needed: "+ (stopTime1 - startTime1));
        System.out.println(" ArrayList needed: "+ (stopTime2 - startTime2));

    }


    public static void fillLinkedList(int number){

        LinkedList<Integer> list = new LinkedList<Integer>();
        ListIterator<Integer> it = list.listIterator();
        int i = 0;
        while(i++<number){
            it.add(i);
        }
    //  System.out.println("LinkedList size: "+list.size());

    }


    public static void fillArrayList(int number){
        ArrayList<Integer> list = new ArrayList<Integer>();
        ListIterator<Integer> it = list.listIterator();
        int i = 0;
        while(i++<number){
            it.add(i);
        }
    //  System.out.println("ArrayList size: "+list.size());
    }

测量给出:

number            10,000     100,000     500,000      1,000,000     5,000,000

ArrayList            7         17         60             77           170

LinkedList           7         21         89             838          4127

我注意到元素的增加显着损害了LinkedList的性能,而ArrayList表现出了相当好的行为。我明白了什么吗?

3 个答案:

答案 0 :(得分:6)

在容器末尾添加元素或非常接近时,

ArrayList更快,因为它不需要移动许多元素。在中间或开头添加时速度很慢。我将你的循环改为:

    while(i++<number){
        it.add(i);
        if(i%2 == 0)
            it.previous();
    }

现在,it将始终指向list的中间位置。使用此基准,LinkedList要快得多。结果为200000:

LinkedList needed: 47
ArrayList needed: 4702

答案 1 :(得分:0)

在开头或中间(数组或列表)中的插入和删除是列表击败数组的位置。

答案 2 :(得分:-1)

据我了解,LinkedList的好处是将值插入给定索引(例如,中间或开头)。 ArrayList不会因顺序插入而丢失,因为它不必将元素移位。

如上所述填充了您的列表后,请查看您在不同位置插入的位置。我已修改您的示例以显示LinkedList获胜的示例(至少在我的设置上):

public static void main(String[] args) {

    int number = 5000000;

    LinkedList<Integer> llist = new LinkedList<Integer>();
    ArrayList<Integer> alist = new ArrayList<Integer>();

    long startTime1 = System.nanoTime();
    fillLinkedList(number, llist);
    long stopTime1 = System.nanoTime();

    long startTime2 = System.nanoTime();
    fillArrayList(number, alist);
    long stopTime2 = System.nanoTime();

    System.out.println(" LinkedList needed: "+ (stopTime1 - startTime1));
    System.out.println(" ArrayList needed: "+ (stopTime2 - startTime2));

    startTime1 = System.nanoTime();
    llist.add(1, 4);
    stopTime1 = System.nanoTime();

    startTime2 = System.nanoTime();
    alist.add(1, 4);
    stopTime2 = System.nanoTime();

    System.out.println(" LinkedList needed: "+ (stopTime1 - startTime1));
    System.out.println(" ArrayList needed: "+ (stopTime2 - startTime2));

}

public static void fillLinkedList(int number, LinkedList<Integer> list){


    ListIterator<Integer> it = list.listIterator();
    int i = 0;
    while(i++<number){
        it.add(i);
    }
    //  System.out.println("LinkedList size: "+list.size());

}


public static void fillArrayList(int number, ArrayList<Integer> list){
    ListIterator<Integer> it = list.listIterator();
    int i = 0;
    while(i++<number){
        it.add(i);
    }
    //  System.out.println("ArrayList size: "+list.size());
}