Skip to content

Commit 0323a81

Browse files
authored
[sum-intarray-recursion] two recursion approaches to summing int-array (#15385)
1 parent 784cb23 commit 0323a81

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

core-java-modules/core-java-lang-6/pom.xml

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
<artifactId>commons-lang3</artifactId>
2424
<version>${commons-lang3.version}</version>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.openjdk.jmh</groupId>
28+
<artifactId>jmh-core</artifactId>
29+
<version>${jmh.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.openjdk.jmh</groupId>
33+
<artifactId>jmh-generator-annprocess</artifactId>
34+
<version>${jmh.version}</version>
35+
</dependency>
2636
</dependencies>
2737
<build>
2838
<plugins>
@@ -39,6 +49,11 @@
3949
<artifactId>mapstruct-processor</artifactId>
4050
<version>${mapstruct.version}</version>
4151
</path>
52+
<path>
53+
<groupId>org.openjdk.jmh</groupId>
54+
<artifactId>jmh-generator-annprocess</artifactId>
55+
<version>${jmh.version}</version>
56+
</path>
4257
</annotationProcessorPaths>
4358
</configuration>
4459
</plugin>
@@ -50,6 +65,7 @@
5065
<maven.compiler.target>17</maven.compiler.target>
5166
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5267
<mapstruct.version>1.6.0.Beta1</mapstruct.version>
68+
<jmh.version>1.37</jmh.version>
5369
</properties>
5470

5571
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.recursivelysumintarray;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class RecursivelySumIntArrayUnitTest {
10+
11+
private static final int[] INT_ARRAY = { 1, 2, 3, 4, 5 };
12+
13+
static int sumIntArray1(int[] array) {
14+
if (array.length == 1) {
15+
return array[0];
16+
} else {
17+
return array[0] + sumIntArray1(Arrays.copyOfRange(array, 1, array.length));
18+
}
19+
}
20+
21+
static int sumIntArray2(int[] array, int index) {
22+
if (index == 0) {
23+
return array[index];
24+
} else {
25+
return array[index] + sumIntArray2(array, index - 1);
26+
}
27+
}
28+
29+
@Test
30+
void whenUsingSumIntArray1_thenGetExpectedResult() {
31+
assertEquals(15, sumIntArray1(INT_ARRAY));
32+
}
33+
34+
@Test
35+
void whenUsingSumIntArray2_thenGetExpectedResult() {
36+
assertEquals(15, sumIntArray2(INT_ARRAY, INT_ARRAY.length - 1));
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.recursivelysumintarray;
2+
3+
import static com.baeldung.recursivelysumintarray.RecursivelySumIntArrayUnitTest.sumIntArray1;
4+
import static com.baeldung.recursivelysumintarray.RecursivelySumIntArrayUnitTest.sumIntArray2;
5+
6+
import java.util.Random;
7+
import java.util.concurrent.TimeUnit;
8+
9+
import org.openjdk.jmh.annotations.Benchmark;
10+
import org.openjdk.jmh.annotations.BenchmarkMode;
11+
import org.openjdk.jmh.annotations.Fork;
12+
import org.openjdk.jmh.annotations.Measurement;
13+
import org.openjdk.jmh.annotations.Mode;
14+
import org.openjdk.jmh.annotations.OutputTimeUnit;
15+
import org.openjdk.jmh.annotations.Param;
16+
import org.openjdk.jmh.annotations.Scope;
17+
import org.openjdk.jmh.annotations.Setup;
18+
import org.openjdk.jmh.annotations.State;
19+
import org.openjdk.jmh.annotations.Warmup;
20+
import org.openjdk.jmh.runner.Runner;
21+
import org.openjdk.jmh.runner.options.Options;
22+
import org.openjdk.jmh.runner.options.OptionsBuilder;
23+
24+
@BenchmarkMode(Mode.AverageTime)
25+
@State(Scope.Thread)
26+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
27+
@Warmup(iterations = 2)
28+
@Fork(1)
29+
@Measurement(iterations = 5)
30+
public class SumArrayBenchmark {
31+
32+
public static void main(String[] args) throws Exception {
33+
Options options = new OptionsBuilder().include(SumArrayBenchmark.class.getSimpleName())
34+
.build();
35+
new Runner(options).run();
36+
}
37+
38+
@Param({ "10", "10000" })
39+
public int size;
40+
int[] array;
41+
42+
@Setup
43+
public void setup() {
44+
var r = new Random();
45+
array = new int[size];
46+
47+
for (int i = 0; i < size; i++) {
48+
array[i] = r.nextInt();
49+
}
50+
}
51+
52+
@Benchmark
53+
public int withArrayCopy() {
54+
return sumIntArray1(array);
55+
}
56+
57+
@Benchmark
58+
public int withoutArrayCopy() {
59+
return sumIntArray2(array, array.length - 1);
60+
}
61+
}

0 commit comments

Comments
 (0)