BAEL-2551 add parallelprefix section in Arrays (#6184)

This commit is contained in:
aietcn 2019-01-22 14:24:38 +08:00 committed by Grzegorz Piwowarek
parent 369655aa02
commit 34907bfb0b
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package com.baeldung.arrays;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.Arrays;
public class ParallelPrefixBenchmark {
private static final int ARRAY_SIZE = 200_000_000;
@State(Scope.Benchmark)
public static class BigArray {
int[] data;
@Setup(Level.Iteration)
public void prepare() {
data = new int[ARRAY_SIZE];
for(int j = 0; j< ARRAY_SIZE; j++) {
data[j] = 1;
}
}
@TearDown(Level.Iteration)
public void destroy() {
data = null;
}
}
@Benchmark
public void largeArrayLoopSum(BigArray bigArray, Blackhole blackhole) {
for (int i = 0; i < ARRAY_SIZE - 1; i++) {
bigArray.data[i + 1] += bigArray.data[i];
}
blackhole.consume(bigArray.data);
}
@Benchmark
public void largeArrayParallelPrefixSum(BigArray bigArray, Blackhole blackhole) {
Arrays.parallelPrefix(bigArray.data, (left, right) -> left + right);
blackhole.consume(bigArray.data);
}
}

View File

@ -1,5 +1,7 @@
package com.baeldung.arrays; package com.baeldung.arrays;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Before;
@ -9,6 +11,7 @@ import org.junit.rules.ExpectedException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.stream.Stream; import java.util.stream.Stream;
public class ArraysUnitTest { public class ArraysUnitTest {
@ -150,4 +153,52 @@ public class ArraysUnitTest {
exception.expect(UnsupportedOperationException.class); exception.expect(UnsupportedOperationException.class);
rets.add("the"); rets.add("the");
} }
@Test
public void givenIntArray_whenPrefixAdd_thenAllAccumulated() {
int[] arri = new int[] { 1, 2, 3, 4};
Arrays.parallelPrefix(arri, (left, right) -> left + right);
assertThat(arri, is(new int[] { 1, 3, 6, 10}));
}
@Test
public void givenStringArray_whenPrefixConcat_thenAllMerged() {
String[] arrs = new String[] { "1", "2", "3" };
Arrays.parallelPrefix(arrs, (left, right) -> left + right);
assertThat(arrs, is(new String[] { "1", "12", "123" }));
}
@Test
public void whenPrefixAddWithRange_thenRangeAdded() {
int[] arri = new int[] { 1, 2, 3, 4, 5 };
Arrays.parallelPrefix(arri, 1, 4, (left, right) -> left + right);
assertThat(arri, is(new int[] { 1, 2, 5, 9, 5 }));
}
@Test
public void whenPrefixNonAssociative_thenError() {
boolean consistent = true;
Random r = new Random();
for (int k = 0; k < 100_000; k++) {
int[] arrA = r.ints(100, 1, 5).toArray();
int[] arrB = Arrays.copyOf(arrA, arrA.length);
Arrays.parallelPrefix(arrA, this::nonassociativeFunc);
for (int i = 1; i < arrB.length; i++) {
arrB[i] = nonassociativeFunc(arrB[i - 1], arrB[i]);
}
consistent = Arrays.equals(arrA, arrB);
if(!consistent) break;
}
assertFalse(consistent);
}
/**
* non-associative int binary operator
*/
private int nonassociativeFunc(int left, int right) {
return left + right*left;
}
} }