Micro benchmarking a loop with different values in JMH

僤鯓⒐⒋嵵緔 提交于 2019-12-10 14:28:41

问题


It is well known that using a loop inside your JMH benchmark is not a good idea because it will be optimized by the JIT compiler and should therefore be avoided. Is there a way to feed my JMH benchmark methods with different values of int inputs (list of inputs) without using a loop.


回答1:


Have a look at this example in the JMH documentation. You can use the @Param annotation on a field in order to tell JMH to inject the values of this annotation:

@Param({"1", "2"})
public int arg;

@Benchmark
public int doBenchmark() {
  return doSomethingWith(arg);
}

The benchmark is then run for both the values 1 and 2.

Note how, if the annotated field is not a String but a primitive, the values are parsed prior to assigment and are assigned in their converted forms. If you have multiple fields with the @Param annotation, JMH will run the benchmark with any possible permutation of the field values.

You can also overridde the value assignment when defining a JMH runner.



来源:https://stackoverflow.com/questions/29012346/micro-benchmarking-a-loop-with-different-values-in-jmh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!