I try to use JMH with parameters, but I get weird results when @Param annotation is used. Here is case with hard-coded function argument:
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 4, time = 4, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class MyBenchmark {
NumberToBinaryArray array = new NumberToBinaryArray();
@Benchmark
public void testMethod() {
array.toBinaryArray2(0);
}
}
This implementation has performance: 2_312_967_772 ops/sec (approx. is 2 billions ops/sec). But if I use arguments for JMH performance slowed down:
public class MyBenchmark {
NumberToBinaryArray array = new NumberToBinaryArray();
@Param({"0"}) <= changed here
private int arg;
@Benchmark
public void testMethod() {
array.toBinaryArray2(arg); <= changed here
}
}
Perfomance is 484_748_276 ops/sec. Why so ?