[sum-intarray-recursion] two recursion approaches to summing int-array (#15385)
This commit is contained in:
+38
@@ -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));
|
||||
}
|
||||
}
|
||||
+61
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user