将10,000,000个元素插入ArrayList和LinkedList的耗时问题

时间:2019-07-16 05:47:12

标签: java list

当我测试将10,000,000元素插入ArrayListLinkedList所花费的时间时,我遇到了一些问题。当我将测试代码放在两个main()函数的两端时,花在LinkedList 上的时间大于大约 ArrayList,大约是两倍。

当我将代码的两端放在相同的main()方法中并且首先插入ArrayList时,ArrayListLinkedList花费的时间更长,这大约是两倍大。我想知道发生什么事了吗?
从理论上讲,ArrayList应该花费更少的时间。

第一段代码:

public static void main(String[] args) {
    long begin2 = System.currentTimeMillis();
    List<Integer> l2 = new LinkedList<>();
    for (int i = 0; i < 10000000; i++) {
        l2.add(Integer.MAX_VALUE);
    }
    long end2 = System.currentTimeMillis();
    System.out.println(end2 - begin2); //Time: 12362

}
public static void main(String[] args) {
    long begin1 = System.currentTimeMillis();
    List<Integer> l1 = new ArrayList<>();
    for (int i = 0; i < 10000000; i++) {
        l1.add(Integer.MAX_VALUE);
    }
    long end1 = System.currentTimeMillis();
    System.out.println(end1 - begin1); //Time: 7531
}

第二段代码:

public static void main(String[] args) {
    long begin1 = System.currentTimeMillis();
    List<Integer> l1 = new ArrayList<>();
    for (int i = 0; i < 10000000; i++) {
        l1.add(Integer.MAX_VALUE);
    }
    long end1 = System.currentTimeMillis();
    System.out.println(end1 - begin1); //Time: 7555

    long begin2 = System.currentTimeMillis();
    List<Integer> l2 = new LinkedList<>();
    for (int i = 0; i < 10000000; i++) {
        l2.add(Integer.MAX_VALUE);
    }
    long end2 = System.currentTimeMillis();
    System.out.println(end2 - begin2); //Time: 3533

 }

下一个代码使我更加困惑,因为两段代码之间的时间差不是很明显:

`public static void main(String[] args) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long begin1 = System.currentTimeMillis();
        List<Integer> l1 = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            l1.add(Integer.MAX_VALUE);
        }
        long end1 = System.currentTimeMillis();
        System.out.println(end1 - begin1); //Time: 4542

        long begin2 = System.currentTimeMillis();
        List<Integer> l2 = new LinkedList<>();
        for (int i = 0; i < 10000000; i++) {
            l2.add(Integer.MAX_VALUE);
        }
        long end2 = System.currentTimeMillis();
        System.out.println(end2 - begin2); //Time: 4325
    }`

1 个答案:

答案 0 :(得分:0)

进一步研究之后,原因似乎与JVM

看看这段代码/结果

public static void main(String[] args) {

        long begin0 = System.currentTimeMillis();
        List<Integer> l0 = new LinkedList<>();
        for (int i = 0; i < 10000000; i++) {
            l0.add(Integer.MAX_VALUE);
        }
        long end0 = System.currentTimeMillis();
        System.out.println(end0 - begin0); //Time: 12992

        long begin1 = System.currentTimeMillis();
        List<Integer> l1 = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            l1.add(Integer.MAX_VALUE);
        }
        long end1 = System.currentTimeMillis();
        System.out.println(end1 - begin1); //Time: 1277

        long begin2 = System.currentTimeMillis();
        List<Integer> l2 = new LinkedList<>();
        for (int i = 0; i < 10000000; i++) {
            l2.add(Integer.MAX_VALUE);
        }
        long end2 = System.currentTimeMillis();
        System.out.println(end2 - begin2); //Time: 1455
    }

请注意,即使是完全相同的代码,第一个循环的运行时间也比第三个循环要长得多。

原因似乎是启动时间开销

Why is the JVM slow to start?

https://en.wikipedia.org/wiki/Java_performance#Startup_time