ArrayList v.s. LinkedList - inserting time

南笙酒味 提交于 2019-12-25 01:13:04

问题


When I run (respectively):

package containers;

import java.util.*;

    public static void main(String[] args) {

    List<Integer> arLst = new ArrayList<Integer>();
    List<Integer> lnLst = new LinkedList<Integer>();

    long start = System.currentTimeMillis();

    for (int i = 0; i < 10000000; i++) {
        arLst.add(i);
    }

    System.out.println("Array list: "+Long.toString(System.currentTimeMillis()-start));

    start = System.currentTimeMillis();

    for (int i = 0; i < 10000000; i++) {
        lnLst.add(i);
    }

    System.out.println("Linked list: "+Long.toString(System.currentTimeMillis()-start));
}

I get roughly the same executing time. I know that Adding time should be faster for LinkedList. I wonder why.. (It makes sense that both for middle insertinon and last elemnt - since arrays know in O(1) where to insert, unlike LinkedList that has to go through the whole list, as I recall).


回答1:


Your code differs from your explanation. However, here is the answer to your question:

ArrayList Vs LinkedList




回答2:


Both lists know where the end of the list is so insertion time is almost the same. I would have expected LinkedList to be slightly slower as it creates a node for every element and uses more memory.

I get

TIntArrayList - 141 ms
ArrayList<Integer> - 810 ms
LinkedList<Integer> - 5190 ms.

TIntArrayList doesn't create an object for each element using the cache more efficiently.




回答3:


Both your lists are ArrayLists... If you change on of them to LinkedList you won't notice a big difference either. Building an ArrayList the way you do has amortized complexity of O(1) per insertion.




回答4:


Ammm.... Maybe this:

List<Integer> lnLst = new ArrayList<>();

should look like this:

List<Integer> lnLst = new LinkedList<>();

And I can not understand what are you trying to measure. I think that you want to measure add perfromance and then your code should look like this:

    public static void main(String[] args) {

        List<Integer> arLst = new ArrayList<Integer>();
        List<Integer> lnLst = new LinkedList<Integer>();

        long start = System.currentTimeMillis();

        for (int i = 0; i < 10000000; i++) {
            arLst.add(i);
        }

        System.out.println("Array list: "+Long.toString(System.currentTimeMillis()-start));

        start = System.currentTimeMillis();

        for (int i = 0; i < 10000000; i++) {
            lnLst.add(i);
        }

        System.out.println("Linked list: "+Long.toString(System.currentTimeMillis()-start));
    }



回答5:


...since arrays know in O(1) where to insert, unlike LinkedList that has to go through the whole list, as I recall).

This isn't correct. Arrays (not ArrayLists) do not have a constant insert time, Java's ArrayList technically doesn't either (The add operation runs in amortized constant time for an ArrayList).

Further, inserting an element into a linked list is O(1):

In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list.

If you are inserting into a sorted list, the complexity becomes O(N).



来源:https://stackoverflow.com/questions/12074441/arraylist-v-s-linkedlist-inserting-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!