package com.bellsw.simple; 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.Threads; import org.openjdk.jmh.annotations.Warmup; import org.openjdk.jmh.annotations.OperationsPerInvocation; @BenchmarkMode(Mode.AverageTime) @Warmup(iterations = 20, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Fork(3) @State(Scope.Thread) public class MathBench { @Benchmark @Threads(1) @OperationsPerInvocation(1000) public double usualPowArray() { double X = 0.7d; // any value double accum = 0.0d; for (int i = 0; i < 1000; i++) { accum += Math.pow(X, X); // X^X X+= 0.5d; } return accum; } @Benchmark @Threads(1) @OperationsPerInvocation(1000) public double usualSinSmallArg() { double X = 0.0d; double accum = 0.0d; for (int i = 0; i < 1000; i++) { accum += Math.sin(X); X+= 0.00001d; } return accum; } @Benchmark @Threads(1) @OperationsPerInvocation(1000) public double usualSinMediumArg() { double X = 100.0d; double accum = 0.0d; for (int i = 0; i < 1000; i++) { accum += Math.sin(X); X+= 1.0d; } return accum; } @Benchmark @Threads(1) @OperationsPerInvocation(1000) public double usualSinLargeArg() { double X = 1000000000.0d; double accum = 0.0d; for (int i = 0; i < 1000; i++) { accum += Math.sin(X); X+= 1000.0d; } return accum; } @Benchmark @Threads(1) @OperationsPerInvocation(1000) public double usualLog() { double X = 5.0d; double accum = 0.0d; for (int i = 0; i < 1000; i++) { accum += Math.log(X); X+= 1.0d; } return accum; } }