Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created December 10, 2015 10:43
Show Gist options
  • Save amaembo/d1cef352b9c26de22d89 to your computer and use it in GitHub Desktop.
Save amaembo/d1cef352b9c26de22d89 to your computer and use it in GitHub Desktop.
MaxTest
package test;
import java.util.concurrent.TimeUnit;
import java.util.stream.*;
import java.util.*;
import java.util.regex.*;
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(3)
@State(Scope.Benchmark)
public class MaxTest {
@Param({"500000"})
private int n;
int[] ints;
@Setup
public void setUp() {
ints = new Random(1).ints(n).toArray();
}
@Benchmark
public int loop() {
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];
return m;
}
@Benchmark
public int stream() {
return Arrays.stream(ints).reduce(Integer.MIN_VALUE, Math::max);
}
}
# JMH 1.11.2 (released 42 days ago)
# VM version: JDK 1.8.0_45, VM 25.45-b02
...
Benchmark (n) Mode Cnt Score Error Units
MaxTest.loop 500000 avgt 30 200.462 ± 0.534 us/op
MaxTest.stream 500000 avgt 30 596.783 ± 6.152 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment