So, here is the simple thing I'm trying to test, that a mod or AND operation is faster (assuming it is equal to two) - this is what hashMap does internally. Is this a correctly written "test"? I have to admit that the insides of jmh and getting the right micro-test after passing all the samples (within the 3rd time, I think) is a rather difficult task. :)
@State(Scope.Thread) @BenchmarkMode(org.openjdk.jmh.annotations.Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class MeasureSpeedModuleVsAnd { public static void main(String[] args) throws Exception { Options opt = new OptionsBuilder() .include(MeasureSpeedModuleVsAnd.class.getSimpleName()) .forks(1) .warmupIterations(1) .measurementIterations(5) .warmupTime(TimeValue.seconds(2)) .build(); new Runner(opt).run(); } @Param({ "16", "32", "256", "1048576" }) public int number_of_buckets; @Param({ "345984", "123456", "111", "98653" }) public int hashcode; @Benchmark public int benchamark_modulo() { return hashcode % number_of_buckets; } @Benchmark public int benchmark_and() { return (number_of_buckets - 1) & hashcode; } }
source share