-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangleSpliteratorExercise.java
More file actions
executable file
·73 lines (60 loc) · 1.96 KB
/
RectangleSpliteratorExercise.java
File metadata and controls
executable file
·73 lines (60 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package spliterators.lesson7.exercise;
import org.openjdk.jmh.annotations.*;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.StreamSupport;
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@State(Scope.Thread)
public class RectangleSpliteratorExercise {
@Param({"100", "1000", "10000"})
public int outerLength;
@Param({"100", "1000", "10000"})
public int innerLength;
public int[][] array;
@Setup
public void setup() {
array = new int[outerLength][];
for (int i = 0; i < array.length; i++) {
int[] inner = new int[innerLength];
array[i] = inner;
for (int j = 0; j < inner.length; j++) {
inner[j] = ThreadLocalRandom.current().nextInt();
}
}
}
@Benchmark
public long baiseline_seq() {
return Arrays.stream(array)
.sequential()
.flatMapToInt(Arrays::stream)
.asLongStream()
.sum();
}
@Benchmark
public long baiseline_par() {
return Arrays.stream(array)
.parallel()
.flatMapToInt(Arrays::stream)
.asLongStream()
.sum();
}
@Benchmark
public long rectangle_seq() {
final boolean parallel = false;
return StreamSupport.intStream(new RectangleSpliterator(array), parallel)
.asLongStream()
.sum();
}
@Benchmark
public long rectangle_par() {
final boolean parallel = true;
return StreamSupport.intStream(new RectangleSpliterator(array), parallel)
.asLongStream()
.sum();
}
}