Last active
June 1, 2023 18:57
-
-
Save amaembo/f683235194a59ae4f10c to your computer and use it in GitHub Desktop.
JMH benchmark for different solutions of http://stackoverflow.com/q/30089761/4856258
This file contains 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
package org.sample; | |
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 = 1, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
@Fork(2) | |
@State(Scope.Benchmark) | |
public class PairTest { | |
@Param({ "10000", "100000", "1000000" }) | |
private int n; | |
private List<Integer> input; | |
@Setup | |
public void setUp() { | |
input = IntStreamEx.of(new Random(1), n, 1, 10000).boxed().toList(); | |
} | |
@Benchmark | |
public void naiveIterator(Blackhole bh) { | |
List<Integer> result = new ArrayList<>(); | |
Integer last = null; | |
for (Integer cur : input) { | |
if (last != null && last < cur) | |
result.add(last); | |
last = cur; | |
} | |
bh.consume(result); | |
} | |
@Benchmark | |
public void naiveGet(Blackhole bh) { | |
List<Integer> result = new ArrayList<>(); | |
for (int i = 0; i < input.size() - 1; i++) { | |
Integer cur = input.get(i), next = input.get(i + 1); | |
if (cur < next) | |
result.add(cur); | |
} | |
bh.consume(result); | |
} | |
@Benchmark | |
public void stream(Blackhole bh) { | |
List<Integer> result = IntStream.range(0, input.size() - 1) | |
.filter(i -> input.get(i) < input.get(i + 1)) | |
.mapToObj(input::get).collect(Collectors.toList()); | |
bh.consume(result); | |
} | |
@Benchmark | |
public void streamParallel(Blackhole bh) { | |
List<Integer> result = IntStream.range(0, input.size() - 1).parallel() | |
.filter(i -> input.get(i) < input.get(i + 1)) | |
.mapToObj(input::get).collect(Collectors.toList()); | |
bh.consume(result); | |
} | |
@Benchmark | |
public void streamEx(Blackhole bh) { | |
List<Integer> result = StreamEx.of(input) | |
.pairMap((a, b) -> a < b ? a : null).nonNull().toList(); | |
bh.consume(result); | |
} | |
@Benchmark | |
public void streamExParallel(Blackhole bh) { | |
List<Integer> result = StreamEx.of(input).parallel() | |
.pairMap((a, b) -> a < b ? a : null).nonNull().toList(); | |
bh.consume(result); | |
} | |
@Benchmark | |
public void reduce(Blackhole bh) { | |
List<Integer> result = new ArrayList<>(); | |
input.stream().reduce((a, b) -> { | |
if (a < b) | |
result.add(a); | |
return b; | |
}); | |
bh.consume(result); | |
} | |
public static Collector<Integer, ?, List<Integer>> collectPrecedingValues() { | |
int[] holder = { Integer.MAX_VALUE }; | |
return Collector.of(ArrayList::new, (l, elem) -> { | |
if (holder[0] < elem) | |
l.add(holder[0]); | |
holder[0] = elem; | |
}, (l1, l2) -> { | |
throw new UnsupportedOperationException("Don't run in parallel"); | |
}); | |
} | |
@Benchmark | |
public void collector(Blackhole bh) { | |
List<Integer> result = input.stream().collect(collectPrecedingValues()); | |
bh.consume(result); | |
} | |
} |
This file contains 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 | |
PairTest.collector 10000 avgt 20 112.513 ± 1.087 us/op | |
PairTest.collector 100000 avgt 20 1404.866 ± 20.467 us/op | |
PairTest.collector 1000000 avgt 20 14387.239 ± 989.853 us/op | |
PairTest.naiveGet 10000 avgt 20 99.787 ± 3.674 us/op | |
PairTest.naiveGet 100000 avgt 20 1084.402 ± 13.105 us/op | |
PairTest.naiveGet 1000000 avgt 20 11424.225 ± 173.268 us/op | |
PairTest.naiveIterator 10000 avgt 20 97.719 ± 7.439 us/op | |
PairTest.naiveIterator 100000 avgt 20 903.956 ± 4.351 us/op | |
PairTest.naiveIterator 1000000 avgt 20 10592.720 ± 798.484 us/op | |
PairTest.reduce 10000 avgt 20 112.101 ± 6.526 us/op | |
PairTest.reduce 100000 avgt 20 1139.526 ± 5.282 us/op | |
PairTest.reduce 1000000 avgt 20 12001.482 ± 137.221 us/op | |
PairTest.stream 10000 avgt 20 146.382 ± 1.394 us/op | |
PairTest.stream 100000 avgt 20 1624.050 ± 14.680 us/op | |
PairTest.stream 1000000 avgt 20 16600.857 ± 193.994 us/op | |
PairTest.streamEx 10000 avgt 20 115.237 ± 2.183 us/op | |
PairTest.streamEx 100000 avgt 20 1247.118 ± 10.179 us/op | |
PairTest.streamEx 1000000 avgt 20 12966.952 ± 166.885 us/op | |
PairTest.streamExParallel 10000 avgt 20 53.415 ± 0.432 us/op | |
PairTest.streamExParallel 100000 avgt 20 516.669 ± 9.072 us/op | |
PairTest.streamExParallel 1000000 avgt 20 5353.370 ± 31.447 us/op | |
PairTest.streamParallel 10000 avgt 20 56.874 ± 1.411 us/op | |
PairTest.streamParallel 100000 avgt 20 582.256 ± 3.028 us/op | |
PairTest.streamParallel 1000000 avgt 20 6120.523 ± 69.305 us/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment