While playing with jmh, I came across a strange thing that I cannot explain.
@BenchmarkMode(Mode.SingleShotTime) @Measurement(iterations = 10, batchSize = Integer.MAX_VALUE) @Warmup(iterations = 5, batchSize = Integer.MAX_VALUE) @State(Scope.Thread) public class Tests { private int value; @Setup(Level.Iteration) public void setUp() { value = 1230; } @Benchmark public boolean testConstModN() { return 12345 % value == 0; } @Benchmark public boolean testNModConst() { return value % 12345 == 0; } }
Results below
Benchmark Mode Cnt Score Error Units Tests.testConstModN ss 10 10.789 ± 0.305 s/op Tests.testNModConst ss 10 7.550 ± 0.067 s/op
I am running on JDK 1.8.0_101, VM 25.101-b13, Intel (R) Core (TM) i7-4770 CPU @ 3.40GHz (family: 0x6, model: 0x3c, stepping: 0x3)
If I set a constant to a value or if I set a value to 0xffffffff , nothing changes.
source share