Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Counting collector test
import java.util.concurrent.TimeUnit;
import java.util.stream.*;
import static java.util.stream.Collectors.*;
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 CountingTest {
private List<Integer> input = new Random(1).ints(100000, 0, Integer.MAX_VALUE).boxed().collect(toList());
@Param({"10", "1000"})
private int groupCount;
@Benchmark
public Map<Integer, Long> countingSeq() {
return input.stream().collect(groupingBy(i -> i%groupCount, counting()));
}
@Benchmark
public Map<Integer, Long> countingPar() {
return input.parallelStream().collect(groupingBy(i -> i%groupCount, counting()));
}
@Benchmark
public Map<Integer, Long> summingSeq() {
return input.stream().collect(groupingBy(i -> i%groupCount, summingLong(t -> 1)));
}
@Benchmark
public Map<Integer, Long> summingPar() {
return input.parallelStream().collect(groupingBy(i -> i%groupCount, summingLong(t -> 1)));
}
}
# JMH 1.10.3 (released 62 days ago)
# VM version: JDK 1.9.0-ea, VM 1.9.0-ea-b80
# VM invoker: C:\Program Files\Java\jdk1.9.0\bin\java.exe
# VM options: <none>
...
Benchmark (groupCount) Mode Cnt Score Error Units
CountingTest.countingPar 10 avgt 30 960.248 ± 28.948 us/op
CountingTest.countingPar 1000 avgt 30 2161.591 ± 50.882 us/op
CountingTest.countingSeq 10 avgt 30 3593.732 ± 87.212 us/op
CountingTest.countingSeq 1000 avgt 30 3924.309 ± 58.757 us/op
CountingTest.summingPar 10 avgt 30 686.258 ± 49.852 us/op
CountingTest.summingPar 1000 avgt 30 2158.131 ± 77.969 us/op
CountingTest.summingSeq 10 avgt 30 2686.243 ± 24.384 us/op
CountingTest.summingSeq 1000 avgt 30 3390.783 ± 39.628 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment