简体   繁体   English

ArrayList vs LinkedList-插入时间

[英]ArrayList v.s. LinkedList - inserting time

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. 我知道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). 我不知道为什么。(这对于中间插入和最后一个插入都是有意义的-因为数组在O(1)中知道插入位置,不像LinkedList那样必须遍历整个列表,我记得)。

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. 我希望LinkedList稍慢一些,因为它为每个元素创建一个节点并使用更多的内存。

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. TIntArrayList不会更有效地使用缓存为每个元素创建一个对象。

Both your lists are ArrayList s... If you change on of them to LinkedList you won't notice a big difference either. 您的两个列表都是ArrayList的...如果将它们更改为LinkedList您也不会注意到很大的不同。 Building an ArrayList the way you do has amortized complexity of O(1) per insertion. 按照您的方式构建ArrayList ,每次插入的摊销复杂度为O(1)。

Your code differs from your explanation. 您的代码与您的解释不同。 However, here is the answer to your question: 但是,这是您问题的答案:

ArrayList Vs LinkedList ArrayList与LinkedList

Ammm.... Maybe this: Ammm ....也许是这样:

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: 我认为您要测量add性能,然后您的代码应如下所示:

    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));
    }

...since arrays know in O(1) where to insert, unlike LinkedList that has to go through the whole list, as I recall). ...因为数组在O(1)中知道插入位置,不像我记得的那样,LinkedList必须遍历整个列表。

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). 数组(不是ArrayList)没有固定的插入时间,Java的ArrayList从技术上也没有(添加操作以ArrayList的摊销固定时间运行)。

Further, inserting an element into a linked list is O(1) : 此外,将元素插入到链表中是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. 除了实现List接口之外,LinkedList类还提供统一命名的方法,以获取,删除和在列表的开头和结尾插入元素。

If you are inserting into a sorted list, the complexity becomes O(N) . 如果要插入排序列表,则复杂度变为O(N)

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

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