How Does The Performance Of Arraylist Compare To A Java Array

when comparing the read access performance of an standard java array, with an array list of equal size which is being itterated over using the new java "for" the performance is almost identical.

results :

to read Array     19284051471732 nano seconds
to read Arraylist 19284059409892 nano seconds

using the following code :

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

abstract class ArrayListvArray {

    private static final Integer INT_VALUE = 10;

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

    public static void main(String... args) {
        ARRAY_LIST = Collections.nCopies(INT_ARRAY.length, INT_VALUE);
        Arrays.fill(INT_ARRAY, INT_VALUE);

        readFromArray();
        readFromArrayList();
    }

    private static long readFromArray() {
        int j;
        long start = System.currentTimeMillis();
        for (int i = 0; i < INT_ARRAY.length; i++) {
            j = INT_ARRAY[i];
        }
        long timeTaken = (System.nanoTime() - start);
        System.out.println("to read Array     " + timeTaken + " nano seconds");
        return timeTaken;
    }

    private static long readFromArrayList() {
        int j;
        long start = System.currentTimeMillis();
        for (Integer integer : ARRAY_LIST) {
            j = integer;
        }
        long timeTaken = (System.nanoTime() - start);
        System.out.println("to read Arraylist " + timeTaken + " nano seconds");
        return timeTaken;
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License