From 82500be545723688d609824cbfbfac006e8a8f31 Mon Sep 17 00:00:00 2001 From: Bahaa El-Din Helmy Date: Wed, 24 May 2023 17:59:59 +0300 Subject: [PATCH] Array vs List Performance in Java (#14114) This commit is related to the article entitled "Array vs List Performance in Java" --- .../ArrayAndArrayListPerformance.java | 116 ++++++++++-------- 1 file changed, 64 insertions(+), 52 deletions(-) diff --git a/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/arrayandlistperformance/ArrayAndArrayListPerformance.java b/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/arrayandlistperformance/ArrayAndArrayListPerformance.java index 3b8fa8c9f3..59540c69b9 100644 --- a/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/arrayandlistperformance/ArrayAndArrayListPerformance.java +++ b/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/arrayandlistperformance/ArrayAndArrayListPerformance.java @@ -1,65 +1,77 @@ package com.baeldung.arrayandlistperformance; + import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; 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) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class ArrayAndArrayListPerformance { - @Benchmark - public void arrayCreation() { - int[] array = new int[1000000]; - } - - @Benchmark - public void arrayListCreation() { - ArrayList list = new ArrayList<>(1000000); - } - - @Benchmark - public void arrayItemSetting() { - int[] array = new int[1000000]; - array[0] = 10; - } - - @Benchmark - public void arrayListItemSetting() { - ArrayList list = new ArrayList<>(1000000); - list.add(0, 10); - } - - @Benchmark - public void arrayItemRetrieval() { - int[] array = new int[1000000]; - array[0] = 10; - int item = array[0]; - } - - @Benchmark - public void arrayListItemRetrieval() { - ArrayList list = new ArrayList<>(1000000); - list.add(0, 10); - int item2 = list.get(0); - } - - @Benchmark - public void arrayCloning() { - int[] array = new int[1000000]; - int[] newArray = array.clone(); - } - - @Benchmark - public void arrayListCloning() { - ArrayList list = new ArrayList<>(1000000); - ArrayList newList = new ArrayList<>(list); - } 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()); + org.openjdk.jmh.runner.Runner runner = new org.openjdk.jmh.runner.Runner(new OptionsBuilder().include(ArrayAndArrayListPerformance.class.getSimpleName()).forks(1).build()); runner.run(); } - } \ No newline at end of file + public static Integer[] array = Collections.nCopies(256, 1).toArray(new Integer[0]); + public static ArrayList list = new ArrayList( + Arrays.asList(array)); + @Benchmark + public Integer[] arrayCreation() { + return new Integer[256]; + } + + @Benchmark + public ArrayList arrayListCreation() { + return new ArrayList<>(256); + } + + @Benchmark + public Integer[] arrayItemsSetting() { + for (int i = 0; i < 256; i++) { + array[i] = i; + } + return array; + } + + @Benchmark + public ArrayList arrayListItemsSetting() { + for (int i = 0; i < 256; i++) { + list.set(i,i); + } + return list; + } + + @Benchmark + public void arrayItemsRetrieval(Blackhole blackhole) { + for (int i = 0; i < 256; i++) { + int item = array[i]; + blackhole.consume(item); + } + } + + @Benchmark + public void arrayListItemsRetrieval(Blackhole blackhole) { + for (int i = 0; i < 256; i++) { + int item = list.get(i); + blackhole.consume(item); + } + } + + @Benchmark + public void arrayCloning(Blackhole blackhole) { + Integer[] newArray = array.clone(); + blackhole.consume(newArray); + } + + @Benchmark + public void arrayListCloning(Blackhole blackhole) { + ArrayList newList = new ArrayList<>(list); + blackhole.consume(newList); + } +} \ No newline at end of file