Skip to content

Instantly share code, notes, and snippets.

@amaembo
Last active Aug 29, 2015
Embed
What would you like to do?
import java.util.concurrent.TimeUnit;
import javax.util.streamex.*;
import java.util.stream.*;
import java.util.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.annotations.*;
@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(2)
@State(Scope.Benchmark)
public class PrimCollector {
@Param({ "100", "10000", "1000000" })
private int n;
private int[] input;
@Setup
public void setUp() {
input = new Random(1).ints(n, -1000, 1000).toArray();
}
@Benchmark
public void sumBySign(Blackhole bh) {
Map<Boolean, Integer> sums = IntStreamEx.of(input)
.collect(IntCollector.partitioningBy(i -> i > 0, IntCollector.summing()));
bh.consume(sums);
}
@Benchmark
public void sumBySignBoxed(Blackhole bh) {
Map<Boolean, Integer> sums = IntStream.of(input).boxed()
.collect(Collectors.partitioningBy(i -> i > 0, Collectors.summingInt(Integer::intValue)));
bh.consume(sums);
}
@Benchmark
public void joining(Blackhole bh) {
String nums = IntStreamEx.of(input).collect(IntCollector.joining(","));
bh.consume(nums);
}
@Benchmark
public void joiningBoxed(Blackhole bh) {
String nums = IntStream.of(input).mapToObj(String::valueOf)
.collect(Collectors.joining(","));
bh.consume(nums);
}
@Benchmark
public void groupByLastDigit(Blackhole bh) {
Map<Integer, int[]> groups = IntStreamEx.of(input)
.collect(IntCollector.groupingBy(i -> i % 10));
bh.consume(groups);
}
@Benchmark
public void groupByLastDigitBoxed(Blackhole bh) {
Map<Integer, List<Integer>> groups = IntStream.of(input).boxed()
.collect(Collectors.groupingBy(i -> i % 10));
bh.consume(groups);
}
}
Benchmark (n) Mode Cnt Score Error Units
PrimCollector.groupByLastDigit 100 avgt 20 2.676 ± 0.008 us/op
PrimCollector.groupByLastDigit 10000 avgt 20 217.997 ± 2.978 us/op
PrimCollector.groupByLastDigit 1000000 avgt 20 22062.558 ± 71.004 us/op
PrimCollector.groupByLastDigitBoxed 100 avgt 20 2.503 ± 0.098 us/op
PrimCollector.groupByLastDigitBoxed 10000 avgt 20 277.244 ± 1.345 us/op
PrimCollector.groupByLastDigitBoxed 1000000 avgt 20 28364.470 ± 356.520 us/op
PrimCollector.joining 100 avgt 20 2.993 ± 0.013 us/op
PrimCollector.joining 10000 avgt 20 326.926 ± 3.930 us/op
PrimCollector.joining 1000000 avgt 20 33277.548 ± 1013.316 us/op
PrimCollector.joiningBoxed 100 avgt 20 5.053 ± 0.020 us/op
PrimCollector.joiningBoxed 10000 avgt 20 580.392 ± 2.971 us/op
PrimCollector.joiningBoxed 1000000 avgt 20 63284.896 ± 828.737 us/op
PrimCollector.sumBySign 100 avgt 20 0.192 ± 0.001 us/op
PrimCollector.sumBySign 10000 avgt 20 13.161 ± 0.016 us/op
PrimCollector.sumBySign 1000000 avgt 20 1411.781 ± 71.582 us/op
PrimCollector.sumBySignBoxed 100 avgt 20 0.775 ± 0.002 us/op
PrimCollector.sumBySignBoxed 10000 avgt 20 72.342 ± 0.306 us/op
PrimCollector.sumBySignBoxed 1000000 avgt 20 8005.887 ± 32.117 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment