From 0323a8168c7ecf9f0991c5c0116272ba95734be3 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Wed, 13 Dec 2023 04:38:31 +0100 Subject: [PATCH] [sum-intarray-recursion] two recursion approaches to summing int-array (#15385) --- core-java-modules/core-java-lang-6/pom.xml | 16 +++++ .../RecursivelySumIntArrayUnitTest.java | 38 ++++++++++++ .../SumArrayBenchmark.java | 61 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 core-java-modules/core-java-lang-6/src/test/java/com/baeldung/recursivelysumintarray/RecursivelySumIntArrayUnitTest.java create mode 100644 core-java-modules/core-java-lang-6/src/test/java/com/baeldung/recursivelysumintarray/SumArrayBenchmark.java diff --git a/core-java-modules/core-java-lang-6/pom.xml b/core-java-modules/core-java-lang-6/pom.xml index 54035c1eb0..a47ed459f6 100644 --- a/core-java-modules/core-java-lang-6/pom.xml +++ b/core-java-modules/core-java-lang-6/pom.xml @@ -23,6 +23,16 @@ commons-lang3 ${commons-lang3.version} + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + @@ -39,6 +49,11 @@ mapstruct-processor ${mapstruct.version} + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + @@ -50,6 +65,7 @@ 17 UTF-8 1.6.0.Beta1 + 1.37 \ No newline at end of file diff --git a/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/recursivelysumintarray/RecursivelySumIntArrayUnitTest.java b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/recursivelysumintarray/RecursivelySumIntArrayUnitTest.java new file mode 100644 index 0000000000..fc61a806e1 --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/recursivelysumintarray/RecursivelySumIntArrayUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.recursivelysumintarray; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class RecursivelySumIntArrayUnitTest { + + private static final int[] INT_ARRAY = { 1, 2, 3, 4, 5 }; + + static int sumIntArray1(int[] array) { + if (array.length == 1) { + return array[0]; + } else { + return array[0] + sumIntArray1(Arrays.copyOfRange(array, 1, array.length)); + } + } + + static int sumIntArray2(int[] array, int index) { + if (index == 0) { + return array[index]; + } else { + return array[index] + sumIntArray2(array, index - 1); + } + } + + @Test + void whenUsingSumIntArray1_thenGetExpectedResult() { + assertEquals(15, sumIntArray1(INT_ARRAY)); + } + + @Test + void whenUsingSumIntArray2_thenGetExpectedResult() { + assertEquals(15, sumIntArray2(INT_ARRAY, INT_ARRAY.length - 1)); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/recursivelysumintarray/SumArrayBenchmark.java b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/recursivelysumintarray/SumArrayBenchmark.java new file mode 100644 index 0000000000..7d6a612a8d --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/recursivelysumintarray/SumArrayBenchmark.java @@ -0,0 +1,61 @@ +package com.baeldung.recursivelysumintarray; + +import static com.baeldung.recursivelysumintarray.RecursivelySumIntArrayUnitTest.sumIntArray1; +import static com.baeldung.recursivelysumintarray.RecursivelySumIntArrayUnitTest.sumIntArray2; + +import java.util.Random; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@BenchmarkMode(Mode.AverageTime) +@State(Scope.Thread) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 2) +@Fork(1) +@Measurement(iterations = 5) +public class SumArrayBenchmark { + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder().include(SumArrayBenchmark.class.getSimpleName()) + .build(); + new Runner(options).run(); + } + + @Param({ "10", "10000" }) + public int size; + int[] array; + + @Setup + public void setup() { + var r = new Random(); + array = new int[size]; + + for (int i = 0; i < size; i++) { + array[i] = r.nextInt(); + } + } + + @Benchmark + public int withArrayCopy() { + return sumIntArray1(array); + } + + @Benchmark + public int withoutArrayCopy() { + return sumIntArray2(array, array.length - 1); + } +} \ No newline at end of file