1

Java array vs Java ArrayList: I wrote a program to compare the time it takes to store a Java array and ArrayList of size MAX with random numbers. Are these results coming out right? Is ArrayList supposed to be faster?

package arraylists;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;

public class arraylists {

    public final static int MAX = 10000;
    static Random rand = new Random(); 
    static int rand_int1 = rand.nextInt(1000); 
    static int rand_int2 = rand.nextInt(1000);


    public static void arrayFunc(int a1 ) {

        int [] arry = new int[a1];

        for(int i = 0; i < a1; i++) {
            arry[i] =  rand_int1;
        }

    }

    public static void arrayList(int a2) {

        ArrayList<Integer> num = new ArrayList<Integer>();

        for(int i = 0; i < a2; i++) {
            num.add(rand_int2);
        }

}

    public static void main(String args[]) {


        for(int i = 0; i < 10; i++){    
            int num;
            System.out.println("Enter the number of iterations: ");
            Scanner input = new Scanner(System.in);
            num = input.nextInt();



            long start1 = System.nanoTime();

            for(int j = 0; j < num; j++) {

                arrayFunc(MAX);

            }

            long end1 = System.nanoTime();
            long timeElapsed1 = end1 - start1;

            long start2 = System.nanoTime();

            for(int j = 0; j < num; j++) {

                arrayFunc(MAX);

            }

            long end2 = System.nanoTime();
            long timeElapsed2 = end2 - start2;

            System.out.println("Java Array execution time (in nanonseconds): " + timeElapsed1);
            System.out.println("Java Array List execution time (in nanoseconds): " + timeElapsed2);
        }


    }

}

Java Array vs Java Array List: I wrote a program to compare the time it takes to store a java array and array list of size MAX with random numbers, are these numbers coming out right? is array list suppose to be faster?

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156

4 Answers4

0

Both of your tests call arrayFunc(). Your arrayList() method is never used.

After correcting your test, I get the following output:

Enter the number of iterations: 
10
Java Array execution time (in nanoseconds): 7703329
Java Array List execution time (in nanoseconds): 49696074

This indicates that the array version is about 6.45 times faster. I suspect that the bulk of that difference stems from the fact that the array that is backing the ArrayList needs to be re-dimensioned several times. This can be mitigated by specifying the required capacity for the ArrayList beforehand:

ArrayList<Integer> num = new ArrayList<Integer>(a2);

Cursory tests indicate that this will speed up your ArrayList method by a factor of 2.


Note that there might also be some concerns about the methods used to benchmark, but I expect that even with correct benchmarking, you will get similar results.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
0

ArrayList is backed by an array (plus the logic to create a new array and migrate references when the list outgrow the array).

So, array is faster but it has a lower level interface, you have to manage the size and manage to update the indexes when you remove or insert in the middle of the list.

Unless required, or you are doing really low level programming, stick with the List interface.

Jonas Fagundes
  • 1,519
  • 1
  • 11
  • 18
0

When we known exactly numbers of elements and if this value is not change, we should use array cause it will faster. ArrayList is built base on array

tn-nguyen
  • 127
  • 5
0

In my opinion: YES, operating ArrayList is more efficient.

After tests of my algorithm solving "Min Window Substring" at CODERBYTE example, where:

Input - N="aaffsfsfasfasfasfasfasfacasfafe", K="fafsf"

Output - "affsf"

I got the following results:

  • using multi dimensional Arrays Time elapsed - hr:0,min:49,sec:3
  • using multi dimensional ArrayLits Time elapsed - hr:0,min:47,sec:9

Source of tests: https://github.com/AWieclawski/edu.awieclawski.chlng/tree/master/src/min/window/substring