简体   繁体   English

Java,添加时间LinkedList vs ArrayList

[英]Java, Adding time LinkedList vs ArrayList

just adding Integer numbers to Arraylist and Linkedlist, to the last position, why adding in arrayList is faster then the linkedlist?只是将 Integer 数字添加到 Arraylist 和 Linkedlist,到最后的 position,为什么添加 Z8DC4BB7771CF6C82F6C8 比链接列表更快? I compiled many many times, and adding in arraylist is faster, why?我编译了很多次,在arraylist中加入速度更快,为什么?

As I know, ArrayList copies an array by 2^n+1 size.据我所知, ArrayList 将数组复制 2^n+1 大小。 while linkedlist changes only the Node而linkedlist只改变节点

class Test1 {

public static void main(String[] args) {
    ArrayList<Integer> arrayList = new ArrayList<>();
    LinkedList<Integer> linkedList = new LinkedList<>();

    addToList(arrayList);
    System.out.println("-----------------");
    addToList(linkedList);

}

public static void addToList(List list) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < 5_000_000; i++) {
        list.add(i);
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);

}

}

When you add to an ArrayList , you just have to store the integer in the backing array.当您添加到ArrayList时,您只需将 integer 存储在后备数组中。 Every once in a while, when the backing array fills up, you have to allocate a new array and copy all the old items to the new array.每隔一段时间,当后备数组填满时,您必须分配一个新数组并将所有旧项目复制到新数组中。 Given 5 million integers, you'll have to do that allocate-and-copy about 20 times (depends on the initial size of the list).给定 500 万个整数,您将不得不进行大约 20 次分配和复制(取决于列表的初始大小)。

To add to a linked list, every addition requires that you:要添加到链表,每次添加都需要您:

  1. Allocate memory for and initialize a new linked list node.分配 memory 并初始化一个新的链表节点。
  2. Link the new node to the end of the list.将新节点链接到列表的末尾。

All that extra work, for both the ArrayList and the LinkedList is done behind the scenes, by the add method. ArrayListLinkedList的所有额外工作都是在幕后通过add方法完成的。

The overhead for 5 million linked list node allocations and links is higher than the overhead for 5 million ArrayList insertions, even when you count the 20 or so allocate-and-copy operations that the ArrayList has to make when it fills up. 500 万个链表节点分配和链接的开销高于 500 万ArrayList插入的开销,即使您计算ArrayList在填满时必须执行的 20 次左右的分配和复制操作也是如此。

So, whereas each operation takes constant time (although in the ArrayList case it's really amortized constant time), the constant for appending to a linked list is higher than the constant for appending to an ArrayList .因此,尽管每个操作都需要恒定时间(尽管在ArrayList的情况下,它实际上是摊销的恒定时间),但附加到链表的常数高于附加到ArrayList的常数。

ArrayList does not copy the whole array every time, it creates a new array doubled in size every time there is no more space in the current array. ArrayList不会每次都复制整个数组,它会在当前数组中没有更多空间时创建一个大小翻倍的新数组。

So if you have an ArrayList with 3 elements, the size of the underlying array is probably 4. Therefore, adding a new element does not require creating a new array and copying the 3 values, and thus has time complexity of O(1).因此,如果您有一个具有 3 个元素的ArrayList ,则底层数组的大小可能为 4。因此,添加新元素不需要创建新数组并复制 3 个值,因此时间复杂度为 O(1)。

They are both O(1) now, but ArrayList only needs to put the value in the correct place in memory, whereas LinkedList needs to allocate new space (for the new node).它们现在都是 O(1),但是ArrayList只需要将值放在 memory 中的正确位置,而LinkedList需要分配新空间(用于新节点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM