Skip to content

Instantly share code, notes, and snippets.

@isopov
Created June 21, 2016 13:51
Show Gist options
  • Save isopov/40309294f1afb668ae86533c880cbb54 to your computer and use it in GitHub Desktop.
Save isopov/40309294f1afb668ae86533c880cbb54 to your computer and use it in GitHub Desktop.
import java.util.concurrent.TimeUnit;
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.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
//Benchmark Mode Cnt Score Error Units
//PowBenchmark.cross2 avgt 5 2.200 ± 0.102 ns/op
//PowBenchmark.cross3 avgt 5 2.322 ± 0.010 ns/op
//PowBenchmark.cross4 avgt 5 2.482 ± 0.101 ns/op
//PowBenchmark.cross5 avgt 5 2.671 ± 0.035 ns/op
//PowBenchmark.pow2 avgt 5 2.184 ± 0.057 ns/op
//PowBenchmark.pow3 avgt 5 64.120 ± 0.326 ns/op
//PowBenchmark.pow4 avgt 5 61.893 ± 0.173 ns/op
//PowBenchmark.pow5 avgt 5 61.924 ± 0.331 ns/op
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
public class PowBenchmark {
private double i = 437.0;
@Benchmark
public double cross2() {
return i * i;
}
@Benchmark
public double pow2() {
return Math.pow(i, 2);
}
@Benchmark
public double cross3() {
return i * i * i;
}
@Benchmark
public double pow3() {
return Math.pow(i, 3);
}
@Benchmark
public double cross4() {
return i * i * i * i;
}
@Benchmark
public double pow4() {
return Math.pow(i, 4);
}
@Benchmark
public double cross5() {
return i * i * i * i * i;
}
@Benchmark
public double pow5() {
return Math.pow(i, 5);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(".*" + PowBenchmark.class.getSimpleName() + ".*")
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment