比较:LinkedList的运行时addFirst()与ArrayList add(0,item)

时间:2013-06-20 01:06:40

标签: java testing arraylist big-o timing

我和我的伙伴正在尝试编写LinkedList数据结构。我们已经完成了数据结构,并且它可以正常运行所有必需的方法。我们需要对LinkedList类中的addFirst()方法的运行时与Java的ArrayList结构的add(0,item)方法进行比较测试。我们的LinkedList数据结构的addFirst()方法的预期复杂性是O(1)常量。这在我们的测试中是正确的。在定时ArrayList add()方法时,我们期望O(N)的复杂性,但我们再次收到大约O(1)常数的复杂性。这似乎是一个奇怪的差异,因为我们正在使用Java的ArrayList。我们认为我们的时序结构可能存在问题,如果任何人能够帮助我们确定问题,我们将非常感激。下面列出了两种方法时序的Java代码:

public class timingAnalysis {

public static void main(String[] args) {

    //timeAddFirst();
    timeAddArray();

}

public static void timeAddFirst()
{
    long startTime, midTime, endTime;
    long timesToLoop = 10000;
    int inputSize = 20000;

    MyLinkedList<Long> linkedList = new MyLinkedList<Long>();

    for (; inputSize <= 1000000; inputSize = inputSize + 20000)
    {
        // Clear the collection so we can add new random
        // values.
        linkedList.clear();

        // Let some time pass to stabilize the thread.
        startTime = System.nanoTime();

        while (System.nanoTime() - startTime < 1000000000)
        {   }

        // Start timing.
        startTime = System.nanoTime();

        for (long i = 0; i < timesToLoop; i++)
            linkedList.addFirst(i);

        midTime = System.nanoTime();

        // Run an empty loop to capture the cost of running the loop.
        for (long i = 0; i < timesToLoop; i++) 
        {} // empty block

        endTime = System.nanoTime();

        // Compute the time, subtract the cost of running the loop from 
        // the cost of running the loop and computing the removeAll method.
        // Average it over the number of runs.
        double averageTime = ((midTime - startTime) - (endTime - midTime)) / timesToLoop;

        System.out.println(inputSize + " " + averageTime);
    }

}

public static void timeAddArray()
{
    long startTime, midTime, endTime;
    long timesToLoop = 10000;
    int inputSize = 20000;

    ArrayList<Long> testList = new ArrayList<Long>();

    for (; inputSize <= 1000000; inputSize = inputSize + 20000)
    {
        // Clear the collection so we can add new random
        // values.
        testList.clear();

        // Let some time pass to stabilize the thread.
        startTime = System.nanoTime();

        while (System.nanoTime() - startTime < 1000000000)
        {   }

        // Start timing.
        startTime = System.nanoTime();

        for (long i = 0; i < timesToLoop; i++)
            testList.add(0, i);

        midTime = System.nanoTime();

        // Run an empty loop to capture the cost of running the loop.
        for (long i = 0; i < timesToLoop; i++) 
        {} // empty block

        endTime = System.nanoTime();

        // Compute the time, subtract the cost of running the loop from 
        // the cost of running the loop and computing the removeAll method.
        // Average it over the number of runs.
        double averageTime = ((midTime - startTime) - (endTime - midTime)) / timesToLoop;

        System.out.println(inputSize + " " + averageTime);
    }

}

}

2 个答案:

答案 0 :(得分:2)

您想要测试不同的inputSize,但是您要执行测试timesToLoop次的操作,这是不变的。当然,这需要相同的时间。你应该使用:

for (long i = 0; i < inputSize; i++)
    testList.add(0, i);

答案 1 :(得分:-1)

据我所知,Arraylist在O(1)时间内添加操作,因此实验结果是正确的。我认为arrayList add方法的常量时间是分摊的常量时间。

根据java doc: 添加n个元素需要O(N)时间,这就是为此添加的摊销常数时间。