Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created February 6, 2016 07:36
Show Gist options
  • Save amaembo/b12599b8e61d805dcc26 to your computer and use it in GitHub Desktop.
Save amaembo/b12599b8e61d805dcc26 to your computer and use it in GitHub Desktop.
package test;
import java.util.concurrent.TimeUnit;
import one.util.streamex.*;
import java.util.stream.*;
import java.util.function.*;
import java.util.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.annotations.*;
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(3)
@State(Scope.Benchmark)
public class GroupRunsTest {
@Param({"1000", "100000", "1000000"})
int n;
@Param({"true", "false"})
boolean serial;
int[] input;
IntUnaryOperator next;
@Setup
public void setup() {
if(serial) {
// input contains many series
input = new Random(1).ints(0, 10).flatMap(x -> IntStream.range(0, x)).limit(n).toArray();
} else {
// input is random: series are only accidental
input = new Random(1).ints(n, 0, 10).toArray();
}
next = i -> i + 1;
}
@Benchmark
public Deque<Deque<Integer>> plain() {
IntStream seq = IntStream.of(input);
Deque<Deque<Integer>> r = new ArrayDeque<>(Collections.singleton(new ArrayDeque<>()));
seq.filter(i -> !r.getLast().isEmpty() && r.getLast().getLast() + 1 != i || !r.getLast().add(i))
.forEach(i -> r.add(new ArrayDeque<>(Collections.singleton(i))));
return r;
}
@Benchmark
public List<List<Integer>> streamEx() {
return IntStreamEx.of(input).boxed().groupRuns((i1, i2) -> next.applyAsInt(i1) == i2).toList();
}
@Benchmark
public List<List<Integer>> streamExParallel() {
return IntStreamEx.of(input).parallel().boxed().groupRuns((i1, i2) -> next.applyAsInt(i1) == i2).toList();
}
}
# JMH 1.11.2 (released 100 days ago, please consider updating!)
# VM version: JDK 1.8.0_45, VM 25.45-b02
...
Benchmark (n) (serial) Mode Cnt Score Error Units
GroupRunsTest.plain 1000 true avgt 30 17,622 ± 0,080 us/op
GroupRunsTest.plain 1000 false avgt 30 18,533 ± 0,205 us/op
GroupRunsTest.plain 100000 true avgt 30 1979,649 ± 30,602 us/op
GroupRunsTest.plain 100000 false avgt 30 2063,066 ± 7,643 us/op
GroupRunsTest.plain 1000000 true avgt 30 19904,397 ± 187,035 us/op
GroupRunsTest.plain 1000000 false avgt 30 24862,988 ± 1670,014 us/op
GroupRunsTest.streamEx 1000 true avgt 30 17,010 ± 0,209 us/op
GroupRunsTest.streamEx 1000 false avgt 30 17,066 ± 0,705 us/op
GroupRunsTest.streamEx 100000 true avgt 30 2026,392 ± 163,430 us/op
GroupRunsTest.streamEx 100000 false avgt 30 1622,729 ± 15,577 us/op
GroupRunsTest.streamEx 1000000 true avgt 30 20064,932 ± 203,330 us/op
GroupRunsTest.streamEx 1000000 false avgt 30 19449,919 ± 897,298 us/op
GroupRunsTest.streamExParallel 1000 true avgt 30 28,201 ± 0,463 us/op
GroupRunsTest.streamExParallel 1000 false avgt 30 31,578 ± 0,685 us/op
GroupRunsTest.streamExParallel 100000 true avgt 30 1247,100 ± 41,272 us/op
GroupRunsTest.streamExParallel 100000 false avgt 30 1270,670 ± 25,329 us/op
GroupRunsTest.streamExParallel 1000000 true avgt 30 12501,340 ± 813,384 us/op
GroupRunsTest.streamExParallel 1000000 false avgt 30 13350,011 ± 450,967 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment