Last active
August 29, 2015 14:21
-
-
Save amaembo/fe03b2944cbb6e621158 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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