Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array vs List Performance in Java #14078

Merged
merged 1 commit into from
May 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
Expand All @@ -16,44 +18,37 @@ public static void main(String[] args) throws Exception {
org.openjdk.jmh.runner.Runner runner = new org.openjdk.jmh.runner.Runner(new OptionsBuilder().include(ArrayAndArrayListPerformance.class.getSimpleName()).forks(1).build());
runner.run();
}

public static Integer[] array = Collections.nCopies(1000000, 1).toArray(new Integer[0]);
public static ArrayList<Integer> list = new ArrayList<Integer>(
Arrays.asList(array));
@Benchmark
public void arrayCreation(Blackhole blackhole) {
int[] array = new int[1000000];
blackhole.consume(array);
public Integer[] arrayCreation() {
return new Integer[1000000];
}

@Benchmark
public void arrayListCreation(Blackhole blackhole) {
ArrayList<Integer> list = new ArrayList<>(1000000);
blackhole.consume(list);
public ArrayList<Integer> arrayListCreation() {
return new ArrayList<>(1000000);
}

@Benchmark
public void arrayItemsSetting(Blackhole blackhole) {
int[] array = new int[1000000];
public Integer[] arrayItemsSetting() {
for (int i = 0; i < 1000000; i++) {
array[i] = i;
}
blackhole.consume(array);

return array;
}

@Benchmark
public void arrayListItemsSetting(Blackhole blackhole) {
ArrayList<Integer> list = new ArrayList<>(1000000);
public ArrayList<Integer> arrayListItemsSetting() {
for (int i = 0; i < 1000000; i++) {
list.add(i);
}
blackhole.consume(list);
return list;
}

@Benchmark
public void arrayItemsRetrieval(Blackhole blackhole) {
int[] array = new int[1000000];
for (int i = 0; i < 1000000; i++) {
array[i] = i;
}
for (int i = 0; i < 1000000; i++) {
int item = array[i];
blackhole.consume(item);
Expand All @@ -62,10 +57,6 @@ public void arrayItemsRetrieval(Blackhole blackhole) {

@Benchmark
public void arrayListItemsRetrieval(Blackhole blackhole) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 1000000; i++) {
list.add(i);
}
for (int i = 0; i < 1000000; i++) {
int item = list.get(i);
blackhole.consume(item);
Expand All @@ -74,14 +65,12 @@ public void arrayListItemsRetrieval(Blackhole blackhole) {

@Benchmark
public void arrayCloning(Blackhole blackhole) {
int[] array = new int[1000000];
int[] newArray = array.clone();
Integer[] newArray = array.clone();
blackhole.consume(newArray);
}

@Benchmark
public void arrayListCloning(Blackhole blackhole) {
ArrayList<Integer> list = new ArrayList<>(1000000);
ArrayList<Integer> newList = new ArrayList<>(list);
blackhole.consume(newList);
}
Expand Down