Skip to content

Instantly share code, notes, and snippets.

@darichey
Last active March 15, 2019 19:48
Show Gist options
  • Save darichey/9ffef86df8a6a5eff91bc96b25ea669c to your computer and use it in GitHub Desktop.
Save darichey/9ffef86df8a6a5eff91bc96b25ea669c to your computer and use it in GitHub Desktop.
Compare methods for getting the maximum element of a Flux and Stream
Benchmark Mode Threads Samples Score Score Error (99.9%) Unit
com.darichey.test.SortVsMax.testMaxFlux thrpt 1 25 1774.439453 519.242665 ops/s
com.darichey.test.SortVsMax.testMaxStream thrpt 1 25 3554.529721 689.647741 ops/s
com.darichey.test.SortVsMax.testSortedFlux thrpt 1 25 254.954391 46.758502 ops/s
com.darichey.test.SortVsMax.testSortedStream thrpt 1 25 376.441597 80.782565 ops/s
package com.darichey.test;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import reactor.core.publisher.Flux;
import reactor.math.MathFlux;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
@State(Scope.Benchmark)
public class SortVsMax {
public static final int NUM_LISTS = 1000;
public static final int NUM_ELEMS = 25;
List<List<Integer>> lists = new ArrayList<>(NUM_LISTS);
@Setup
public void setup() {
for (int i = 0; i < NUM_LISTS; i++) {
List<Integer> list = new ArrayList<>(NUM_ELEMS);
for (int j = 0; j < NUM_ELEMS; j++) {
list.add(j, ThreadLocalRandom.current().nextInt());
}
lists.add(i, list);
}
}
@Benchmark
public void testSortedStream(Blackhole bh) {
for (List<Integer> list : lists) {
int i = list.stream().sorted(Integer::compareTo).reduce((f, s) -> s).get();
bh.consume(i);
}
}
@Benchmark
public void testMaxStream(Blackhole bh) {
for (List<Integer> list : lists) {
int i = list.stream().max(Integer::compareTo).get();
bh.consume(i);
}
}
@Benchmark
public void testSortedFlux(Blackhole bh) {
for (List<Integer> list : lists) {
int i = Flux.fromIterable(list).sort(Integer::compareTo).last().block();
bh.consume(i);
}
}
@Benchmark
public void testMaxFlux(Blackhole bh) {
for (List<Integer> list : lists) {
int i = MathFlux.max(Flux.fromIterable(list), Integer::compareTo).block();
bh.consume(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment