Skip to content

Instantly share code, notes, and snippets.

@voronaam
Created April 6, 2018 00:01
Show Gist options
  • Save voronaam/e1f88c4bb948969c9e80afc15482cc6b to your computer and use it in GitHub Desktop.
Save voronaam/e1f88c4bb948969c9e80afc15482cc6b to your computer and use it in GitHub Desktop.
Math pow benchmark
package org.sample;
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.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class MyBenchmark {
@Param({"1", "2", "3", "5", "8", "13", "100"})
public int count;
@Benchmark
public int testPow() {
int delay = (int) Math.min(60, Math.pow(2, count));
return delay;
}
@Benchmark
public int testCmp() {
int delay = count > 5 ? 60 : (1 << count);
return delay;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment