简体   繁体   English

为什么第一次将对象添加到ArrayList或LinkedList需要更多时间?

[英]Why the first time to add object into ArrayList or LinkedList require much more time?

Why the first time to add object into ArrayList or LinkedList require much more time? 为什么第一次将对象添加到ArrayList或LinkedList需要更多时间?

I am testing the performance of ArrayList and LinkedList. 我正在测试ArrayList和LinkedList的性能。 The result is what I expected. 结果是我所期望的。 The ArrayList is generally better for random access and every insertion into LinkedList requires similar time. ArrayList通常更适合随机访问,每次插入LinkedList都需要相似的时间。 But I found that the first time for adding the object into ArrayList or LinkedList requires much more time than other. 但是我发现第一次将对象添加到ArrayList或LinkedList 所需的时间比其他时间要多得多。

I have tested the codes on Mac and Linux , the situation is similar. 我已经在Mac和Linux上测试了代码,情况相似。 You can download the code and the full result from here . 您可以从此处下载代码和完整结果。

java PlayArrayList       
0 to 10, elapsedTime: 716362
10 to 20, elapsedTime: 19765
20 to 30, elapsedTime: 10895

$ java PlayLinkedList 
0 to 10, elapsedTime: 704209
10 to 20, elapsedTime: 5867
20 to 30, elapsedTime: 5378

PS: They are measured in nanoseconds. PS:它们以纳秒为单位测量。

/* Untilty Class for measuring elapsed time */
public class BenmarkTimer {
    private long startTime;
    private long endTime;

    /* Getter */
    public long getStartTime(){
        return startTime;
    }

    public long getEndTime(){
        return endTime;
    }

    /* Setter */
    private void setStartTime(long t){
        startTime = t;
    }

    private void setEndTime(long t){
        endTime = t;
    }

    /* Method */
    public void start(){
        setStartTime(System.nanoTime());
    } 

    public long end(){
        setEndTime(System.nanoTime());
        return getDuration();
    }

    public void cancel(){
        setStartTime(0);
        setEndTime(0);
    }

    public long getDuration(){
        return getEndTime() - getStartTime();
    }

    /* Unit testing */
    public static void main (String [] args){
        BenmarkTimer timer = new BenmarkTimer();
        timer.start();
        System.out.println("Hello, World for timer");
        timer.end();
        long t = timer.getDuration();
        System.out.println("Start time "+ timer.getStartTime());
        System.out.println("End time "+ timer.getEndTime());
        System.out.println("Elaped time "+ t);
    }
}


import java.util.*;

public class PlayArrayList{
    private int itemCount;
    private BenmarkTimer timer;
    private List<Integer> list; 

    public PlayArrayList(){
        itemCount = 0;
        timer = new BenmarkTimer();
        list = getList();
    }

    public long addTenIntegers(){
        timer.start();
        for (int i=itemCount; i<(itemCount + 10); i++ ) {
            list.add(i);
        }
        long elapsedTime = timer.end();
        itemCount += 10;

        return elapsedTime;
    }

    public long randomRemove(){
        Random r = new Random();
        int s = list.size();
        int i = r.nextInt(s);

        timer.start();
        list.remove(i);
        return timer.end();
    }

    public String toString(){
        return list.toString();
    }

    /* Factory method */
    protected List<Integer> getList(){
        return new ArrayList<Integer>();
    }

    public static void main (String [] args){
        PlayArrayList playAList = new PlayArrayList();

        /* Add 99 integers */
        long elapsedTime;
        for (int i=0; i<10; i++){
            elapsedTime = playAList.addTenIntegers();
            System.out.print(i*10 + " to " + (i*10+10));
            System.out.println(", elapsedTime: "+ elapsedTime);
        }

        System.out.println("Array content");
        System.out.println(playAList.toString());


        /* Remove 99 integer */
        for (int i=0; i<99; i++){
            elapsedTime = playAList.randomRemove();
            System.out.print("Remove a integer");
            System.out.println(", elapsedTime: "+ elapsedTime);
        }

    }
} 

import java.util.*;

public class PlayLinkedList extends PlayArrayList{
    /* Factory method */
    @Override
    protected List<Integer> getList(){
        return new LinkedList<Integer>();
    }

    public static void main (String [] args){
        PlayLinkedList playAList = new PlayLinkedList();

        /* Add 99 integers */
        long elapsedTime;
        for (int i=0; i<10; i++){
            elapsedTime = playAList.addTenIntegers();
            System.out.print(i*10 + " to " + (i*10+10));
            System.out.println(", elapsedTime: "+ elapsedTime);
        }

        System.out.println("Array content");
        System.out.println(playAList.toString());


        /* Remove 99 integer */
        for (int i=0; i<99; i++){
            elapsedTime = playAList.randomRemove();
            System.out.print("Remove a integer");
            System.out.println(", elapsedTime: "+ elapsedTime);
        }

    }
} 

I guess the main cause of this problem is the rule 4 from here , the initialization effects of new class. 我想这个问题的主要原因是从这里开始的规则4,即新类的初始化效果。 I created another object first to do the operation for initialization. 我首先创建了另一个对象来进行初始化操作。 The class used at the first time always requires longer time for initialization. 第一次使用的类始终需要更长的时间进行初始化。 After that, the duration of the operation will be much shorter. 此后,操作时间将大大缩短。

   PlayArrayList playAListInit = new PlayArrayList();
   long firstTime = playAListInit.addTenIntegers();
   System.out.println("First time: "+ firstTime);

   PlayArrayList playAList = new PlayArrayList();

Then the result will look like: 然后结果将如下所示:

java PlayArrayList
First time: 710000
0 to 10, elapsedTime: 6000
10 to 20, elapsedTime: 12000
20 to 30, elapsedTime: 9000

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

相关问题 为什么LinkedList和ArrayList的运行时间不同 - why the running time of the LinkedList and the ArrayList are not the same 为什么ArrayList比Java中的LinkedList花费更多的时间在开头插入元素 - Why does ArrayList takes more time to insert element in the beginning than LinkedList in java 添加方法的时间比较:ArrayList,LinkedList,ArrayList(使用某些值初始化) - Time comparison of add method for: ArrayList, LinkedList, ArrayList (initialized with some value) 为什么在插入LinkedList和ArrayList时得到关于时间的不同输出 - Why getting the different output regarding the time when inserting to LinkedList and ArrayList ArrayList vs LinkedList-插入时间 - ArrayList v.s. LinkedList - inserting time 测量对 ArrayList 和 LinkedList 进行插入操作所花费的时间 - Measure time taken by insert operation on the ArrayList and LinkedList Java,添加时间LinkedList vs ArrayList - Java, Adding time LinkedList vs ArrayList 为什么第一次运行HttpClient的速度很慢,但是要快得多呢? - why runing HttpClient the first time is slow but much faster then? 首先进行循环需要很多时间 - First for loop take much time UrlConnection API 第一次调用需要更多时间,然后它与 curl.exe 或 postman 相当 - UrlConnection API Call takes much more time the first time, then onwards it is comparable to curl.exe or postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM