Skip to content

Instantly share code, notes, and snippets.

@amaembo
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaembo/19445fbf2419bf04a635 to your computer and use it in GitHub Desktop.
Save amaembo/19445fbf2419bf04a635 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.util.stream.*;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.annotations.*;
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(2)
@State(Scope.Benchmark)
public class MyBenchmark {
@Param({"5000", "50000", "500000"})
private int n;
private int[] ints;
@Setup
public void setup() {
ints = new Random(1).ints(n).toArray();
}
@Benchmark
public void stream(Blackhole bh) {
bh.consume(Arrays.stream(ints).reduce(Integer.MIN_VALUE, Math::max));
}
@Benchmark
public void streamParallel(Blackhole bh) {
bh.consume(Arrays.stream(ints).parallel().reduce(Integer.MIN_VALUE, Math::max));
}
@Benchmark
public void simple(Blackhole bh) {
int[] a = ints;
int e = ints.length;
int m = Integer.MIN_VALUE;
for(int i=0; i < e; i++) if(a[i] > m) m = a[i];
bh.consume(m);
}
}
# JMH 1.10.1 (released 34 days ago)
# VM invoker: C:\Program Files\Java\jdk1.8.0_40\jre\bin\java.exe
Benchmark (n) Mode Cnt Score Error Units
MyBenchmark.simple 5000 avgt 20 1.305 ± 0.080 us/op
MyBenchmark.simple 50000 avgt 20 12.949 ± 0.223 us/op
MyBenchmark.simple 500000 avgt 20 131.875 ± 4.914 us/op
MyBenchmark.stream 5000 avgt 20 5.259 ± 0.051 us/op
MyBenchmark.stream 50000 avgt 20 57.328 ± 5.345 us/op
MyBenchmark.stream 500000 avgt 20 656.991 ± 29.502 us/op
MyBenchmark.streamParallel 5000 avgt 20 8.408 ± 1.852 us/op
MyBenchmark.streamParallel 50000 avgt 20 20.290 ± 3.579 us/op
MyBenchmark.streamParallel 500000 avgt 20 102.484 ± 9.909 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment