0

I tried the following to test the performance of array and ArrayList. I know That arrayList is backed-up by array, but i saw strange behavior while accessing the data from list and array as mentioned below.. I would like to understand why this behavior? Doesn't the creating ArrayList with initial capacity improve performance, or am i doing any mistake?. I did not find any improvement on performance while adding the elements.

The output of below program is given below

time taken to add elements 1
time taken to print elements 229
LIST: time taken to add elements 10 // it is same even i set the initial capacity by passing as arg to constructor.
LIST: time taken to print elements 161

I have omitted the print statements as its taking space.

I see, its a duplicate post, however, posting as it is giving different behavior while retrieving.

package com.samples.misc;

import java.util.ArrayList;
import java.util.List;

public class ArrayTest {

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();
        int a[] = new int[50000];
        for(int i=0;i<a.length;i++)
            a[i] = i;
        long endTime = System.currentTimeMillis();
        System.out.print("time taken to add elements "+(endTime - startTime));
        System.out.println();

        startTime = System.currentTimeMillis();
        for(int i=0;i<a.length;i++)
            System.out.print(a[i]);
        endTime = System.currentTimeMillis();
        System.out.println();
        System.out.println("time taken to print elements "+(endTime - startTime));
        System.out.println();

        startTime = System.currentTimeMillis();
        List<Integer> integers = new ArrayList<Integer>(); 
        //List<Integer> integers = new ArrayList<Integer>(50000); this does not make any diff, it takes same amount of time to add elements, whats the use case of this?
        for(int i=0;i<50000;i++){
            integers.add(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("LIST: time taken to add elements "+(endTime - startTime));
        System.out.println();

        startTime = System.currentTimeMillis();
        for(int i=0;i<a.length;i++)
            System.out.print(integers.get(i));
        endTime = System.currentTimeMillis();
        System.out.println();
        System.out.println("LIST: time taken to print elements "+(endTime - startTime));
    }
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
greengreyblue
  • 389
  • 3
  • 12

1 Answers1

0

it's not really a fair comparison as in the array you're adding int (primitive type) while in the list you're implicitly adding Integer (object), which are more heavyweight. A fair comparison would be Integer[] instead of int[]. Autoboxing will take care of the conversions so this is all you need to change to make for a valid comparison. Likely the print operation requires objects after all, so outputting the array requires more time there creating the objects, whereas the list already has them at that point.

geert3
  • 7,086
  • 1
  • 33
  • 49
  • Done. The following is the result. time taken to add elements 13 time taken to print elements 208 LIST: time taken to add elements 8 LIST: time taken to print elements 145. What i am not clear is though ArrayList is backed-up by array, I see, arrayList is faster, i thought it should be otherway!!. just for FYI, compiler version is 1.6. – greengreyblue Nov 30 '14 at 11:02
  • 1
    @AmidalaSivaKumar Benchmarking is not that simple. You need to take in account some facts (like the warmup time, the optimizations done by the JIT compiler, that `System.currentTimeMillis()` is not very accurate, etc.). Consider using a micro benchmarking framework like JMH. – Alexis C. Nov 30 '14 at 11:05
  • @AmidalaSivaKumar you should write micro benchmarks using [jmh](http://openjdk.java.net/projects/code-tools/jmh/) to get clear results – retroq Nov 30 '14 at 11:08
  • @ZouZou true. Also increasing the number of elements or repeating the test a few 100 times will show that the results will get closer to each other. – geert3 Nov 30 '14 at 11:08