Skip to content

Instantly share code, notes, and snippets.

@itsHobbes
Last active October 17, 2021 23:50
Show Gist options
  • Save itsHobbes/87cbf4ffce197136b6558d5d96ddfc30 to your computer and use it in GitHub Desktop.
Save itsHobbes/87cbf4ffce197136b6558d5d96ddfc30 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
/**
* Benchmark Mode Cnt Score Error Units
* App.forLoop thrpt 30 204677.011 ± 627.430 ops/s
* App.forLoopLong thrpt 30 204995.211 ± 417.456 ops/s
* App.longAdder thrpt 30 18920.561 ± 17.680 ops/s
* App.mapToIntSum thrpt 30 182377.331 ± 382.021 ops/s
* App.mapToIntSumParallel thrpt 30 32893.994 ± 51.316 ops/s
* App.mapToLongSum thrpt 30 161329.976 ± 413.711 ops/s
* App.mapToLongSumParallel thrpt 30 32843.718 ± 69.592 ops/s
* App.reduceSumInt thrpt 30 28115.223 ± 34.760 ops/s
* App.reduceSumIntParallel thrpt 30 24249.391 ± 119.585 ops/s
* App.summingCollectorInt thrpt 30 42028.352 ± 47.739 ops/s
* App.summingCollectorIntParallel thrpt 30 32714.139 ± 42.214 ops/s
* App.summingCollectorLong thrpt 30 29840.492 ± 55.949 ops/s
* App.summingCollectorLongParallel thrpt 30 32761.065 ± 43.750 ops/s
*/
@Fork(value = 1, warmups = 1)
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 30, time = 1)
public class App {
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
@Benchmark
public void summingCollectorInt(BenchmarkState state, Blackhole bh) {
long sum = state.list.stream().collect(Collectors.summingInt(i -> i));
bh.consume(sum);
}
@Benchmark
public void summingCollectorIntParallel(BenchmarkState state, Blackhole bh) {
long sum = state.list.parallelStream().collect(Collectors.summingInt(i -> i));
bh.consume(sum);
}
@Benchmark
public void summingCollectorLong(BenchmarkState state, Blackhole bh) {
long sum = state.list.stream().collect(Collectors.summingLong(i -> i));
bh.consume(sum);
}
@Benchmark
public void summingCollectorLongParallel(BenchmarkState state, Blackhole bh) {
long sum = state.list.parallelStream().collect(Collectors.summingLong(i -> i));
bh.consume(sum);
}
@Benchmark
public void mapToIntSum(BenchmarkState state, Blackhole bh) {
long sum = state.list.stream().mapToInt(Integer::intValue).sum();
bh.consume(sum);
}
@Benchmark
public void mapToIntSumParallel(BenchmarkState state, Blackhole bh) {
long sum = state.list.parallelStream().mapToInt(Integer::intValue).sum();
bh.consume(sum);
}
@Benchmark
public void mapToLongSum(BenchmarkState state, Blackhole bh) {
long sum = state.list.stream().mapToLong(Integer::longValue).sum();
bh.consume(sum);
}
@Benchmark
public void mapToLongSumParallel(BenchmarkState state, Blackhole bh) {
long sum = state.list.parallelStream().mapToLong(Integer::longValue).sum();
bh.consume(sum);
}
@Benchmark
public void longAdder(BenchmarkState state, Blackhole bh) {
LongAdder a = new LongAdder();
state.list.parallelStream().forEach(a::add);
bh.consume(a.intValue());
}
@Benchmark
public void reduceSumInt(BenchmarkState state, Blackhole bh) {
long sum = state.list.stream().reduce(0, Integer::sum);
bh.consume(sum);
}
@Benchmark
public void reduceSumIntParallel(BenchmarkState state, Blackhole bh) {
long sum = state.list.parallelStream().reduce(0, Integer::sum);
bh.consume(sum);
}
@Benchmark
public void forLoop(BenchmarkState state, Blackhole bh) {
long sum = 0;
for (int num : state.list) {
sum += num;
}
bh.consume(sum);
}
@Benchmark
public void forLoopLong(BenchmarkState state, Blackhole bh) {
long sum = 0;
for (long num : state.list) {
sum += num;
}
bh.consume(sum);
}
@State(Scope.Thread)
public static class BenchmarkState {
List<Integer> list = getList();
private List<Integer> getList() {
List<Integer> arr = new ArrayList<>(10_000);
for (int i = 0; i < 10_000; i++) {
arr.add(ThreadLocalRandom.current().nextInt(100_000));
}
return arr;
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.DoubleAdder;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
/**
* Benchmark Mode Cnt Score Error Units
* App.doubleAdder thrpt 30 18293.395 ± 14.682 ops/s
* App.forLoop thrpt 30 142610.783 ± 631.719 ops/s
* App.mapToDoubleSum thrpt 30 26930.080 ± 46.574 ops/s
* App.mapToDoubleSumParallel thrpt 30 29529.145 ± 101.808 ops/s
* App.reduceSumDouble thrpt 30 27448.216 ± 108.012 ops/s
* App.reduceSumDoubleParallel thrpt 30 20857.647 ± 42.839 ops/s
* App.summingCollectorDouble thrpt 30 20930.409 ± 160.458 ops/s
* App.summingCollectorDoubleParallel thrpt 30 30212.752 ± 41.851 ops/s
*/
@Fork(value = 1, warmups = 1)
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 30, time = 1)
public class App {
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
@Benchmark
public void summingCollectorDouble(BenchmarkState state, Blackhole bh) {
double sum = state.list.stream().collect(Collectors.summingDouble(i -> i));
bh.consume(sum);
}
@Benchmark
public void summingCollectorDoubleParallel(BenchmarkState state, Blackhole bh) {
double sum = state.list.parallelStream().collect(Collectors.summingDouble(i -> i));
bh.consume(sum);
}
@Benchmark
public void mapToDoubleSum(BenchmarkState state, Blackhole bh) {
double sum = state.list.stream().mapToDouble(Double::doubleValue).sum();
bh.consume(sum);
}
@Benchmark
public void mapToDoubleSumParallel(BenchmarkState state, Blackhole bh) {
double sum = state.list.parallelStream().mapToDouble(Double::doubleValue).sum();
bh.consume(sum);
}
@Benchmark
public void doubleAdder(BenchmarkState state, Blackhole bh) {
DoubleAdder a = new DoubleAdder();
state.list.parallelStream().forEach(a::add);
bh.consume(a.doubleValue());
}
@Benchmark
public void reduceSumDouble(BenchmarkState state, Blackhole bh) {
double sum = state.list.stream().reduce(0.0, Double::sum);
bh.consume(sum);
}
@Benchmark
public void reduceSumDoubleParallel(BenchmarkState state, Blackhole bh) {
double sum = state.list.parallelStream().reduce(0.0, Double::sum);
bh.consume(sum);
}
@Benchmark
public void forLoop(BenchmarkState state, Blackhole bh) {
double sum = 0;
for (double num : state.list) {
sum += num;
}
bh.consume(sum);
}
@State(Scope.Thread)
public static class BenchmarkState {
List<Double> list = getList();
private List<Double> getList() {
List<Double> arr = new ArrayList<>(10_000);
for (int i = 0; i < 10_000; i++) {
arr.add(ThreadLocalRandom.current().nextDouble(100_000));
}
return arr;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment