Skip to content

Instantly share code, notes, and snippets.

@fxnn
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxnn/a0f987ea062e12b97cc2 to your computer and use it in GitHub Desktop.
Save fxnn/a0f987ea062e12b97cc2 to your computer and use it in GitHub Desktop.
Java Performance of Array vs ArrayList: http://stackoverflow.com/q/30217484/3281722
package de.fxnn.arraylistvsarray;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class Main {
protected enum TestCase {
CREATE_PRIMITIVE_ARRAY,
CREATE_ARRAY_LIST,
MAX_OVER_PRIMITIVE_ARRAY,
MAX_OVER_ARRAY_LIST
}
protected static final int LOOP_SIZE = 1000000;
protected static final Random RANDOM = new Random();
public static void main(String[] args) {
Map<TestCase, List<Double>> result = new EnumMap<>(TestCase.class);
for (TestCase testCase : TestCase.values()) {
result.put(testCase, new ArrayList<>());
}
for (int i = 0; i < 100; i++) {
System.out.println("Iteration " + i);
Map<TestCase, Double> iterationResults = runPerformanceTests();
for (TestCase testCase : iterationResults.keySet()) {
result.get(testCase).add(iterationResults.get(testCase));
}
System.out.println();
}
for (TestCase testCase : result.keySet()) {
double average = 0.0;
for (Double iterationResult : result.get(testCase)) {
average += iterationResult;
}
average = average / result.get(testCase).size();
System.out.println("Test case " + testCase + " average time: " + average + " ms");
}
}
private static Map<TestCase, Double> runPerformanceTests() {
Map<TestCase, Double> result = new EnumMap<>(TestCase.class);
long millis;
millis = System.currentTimeMillis();
ArrayList<Integer> arrayList = createArrayList(LOOP_SIZE);
millis = System.currentTimeMillis() - millis;
System.out.println("Creating array list with " + LOOP_SIZE + " elements took " + millis + " ms");
result.put(TestCase.CREATE_ARRAY_LIST, (double) millis);
millis = System.currentTimeMillis();
loopOverArrayList(arrayList);
millis = System.currentTimeMillis() - millis;
System.out.println("Loop over array list with " + LOOP_SIZE + " elements took " + millis + " ms");
result.put(TestCase.MAX_OVER_ARRAY_LIST, (double) millis);
millis = System.currentTimeMillis();
int[] array = createArray(LOOP_SIZE);
millis = System.currentTimeMillis() - millis;
System.out.println("Creating array with " + LOOP_SIZE + " elements took " + millis + " ms");
result.put(TestCase.CREATE_PRIMITIVE_ARRAY, (double) millis);
millis = System.currentTimeMillis();
loopOverArray(array);
millis = System.currentTimeMillis() - millis;
System.out.println("Loop over array with " + LOOP_SIZE + " elements took " + millis + " ms");
result.put(TestCase.MAX_OVER_PRIMITIVE_ARRAY, (double) millis);
return result;
}
private static int loopOverArray(int[] array) {
int max = Integer.MIN_VALUE;
for (int element : array) {
max = Math.max(element, max);
}
return max;
}
private static int loopOverArrayList(ArrayList<Integer> arrayList) {
int max = Integer.MIN_VALUE;
for (Integer element : arrayList) {
max = Math.max(element, max);
}
return max;
}
private static int[] createArray(int loopSize) {
int[] result = new int[loopSize];
for (int i = 0; i < loopSize; i++) {
result[i] = RANDOM.nextInt();
}
return result;
}
private static ArrayList<Integer> createArrayList(int loopSize) {
ArrayList<Integer> result = new ArrayList<>(loopSize);
for (int i = 0; i < loopSize; i++) {
result.add(RANDOM.nextInt());
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment