This discussion is archived
3 Replies Latest reply: Mar 21, 2012 3:01 AM by gimbal2 RSS

Array vs ArrayList

893253 Explorer
Currently Being Moderated
Can anyone please helpme with :
WHich is better in terms of faster execution Array or ArrayList? And Why so?
  • 1. Re: Array vs ArrayList
    796440 Guru
    Currently Being Moderated
    J2EE_Life wrote:
    Can anyone please helpme with :
    WHich is better in terms of faster execution Array or ArrayList?
    Neither is significantly faster than the other.

    You are almost certainly falling victim to the common mistake of premature optimization.

    In most cases you will prefer a List over an array, for design and functionality reasons. Performance will not enter into it.
  • 2. Re: Array vs ArrayList
    925341 Newbie
    Currently Being Moderated
    The performance will very much depend on the VM involved, and a variety of other considerations.

    The ArrayList is backed by an array, when storing data into the ArrayList, more work is performed, for example

    A nullity check (to see whether the ArrayList reference is non-null).

    A bounds check — to handle the resizing logic, to resize the array that backs its. This array is resized in chunks.
    so the size of the list is usually smaller than the length of the array.

    Potentially a virtual method indirection, depending on whether the JIT has managed to inline the call

    Its worth noting that usually the performance of one single method call doesn't matter much, usually its not worth sacrificing good design for few micro-seconds. Only optimize your fast paths, its likely most of your code will be run infrequently.

    To read from an ArrayLists takes three time longer than reading from an Array.

    results for iterating over the arrays via the for loop:

    To read Integer[] takes 1133201 nano seconds
    To read ArrayList takes 3669686 nano seconds

    using the following code :



    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;

    public class ArrayListvArray {

    private static final Integer INT_VALUE = 1000;

    public static Integer[] INT_ARRAY = new Integer[50000];
    public static List<Integer> ARRAY_LIST;

    public static void main(String... args) {

    // as the JVM warms up the times will improved

    for(;;) {
    ARRAY_LIST = new ArrayList(Collections.nCopies(INT_ARRAY.length, INT_VALUE));
    Arrays.fill(INT_ARRAY, INT_VALUE);

    readFromArray();
    readFromArrayList();
    }

    }

    private static long readFromArray() {

    Integer j;
    long start = System.nanoTime();

    for (int i = 0; i < INT_ARRAY.length; i++) {
    j = INT_ARRAY; //
    }
    long timeTaken = (System.nanoTime() - start);
    System.out.println("To read " + INT_ARRAY.getClass().getSimpleName() + " takes " + timeTaken + " nano seconds");
    return timeTaken;
    }

    private static long readFromArrayList() {

    Integer j;
    long start = System.nanoTime();
    for (int i = 0; i < INT_ARRAY.length; i++) {
    j = ARRAY_LIST.get(i);
    }
    long timeTaken = (System.nanoTime() - start);

    System.out.println("To read " + ARRAY_LIST.getClass().getSimpleName() + " takes " + timeTaken + " nano seconds");
    return timeTaken;
    }
    }
  • 3. Re: Array vs ArrayList
    gimbal2 Guru
    Currently Being Moderated
    this thread is 2 months old dude. And the code you posted doesn't compile; the next time use \
     tags so it is properly formatted and the forum doesn't try to interpret special characters.
    
    Still it triggered me to check something else out: how does using an Iterator compare to simply looping over the list index based like you do here. A lot it turned out: an Iterator is an order of a magnitude slower.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points