JMH:不考虑内部方法时间 [英] JMH: don't take into account inner method time
本文介绍了JMH:不考虑内部方法时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有:这样的方法:
@GenerateMicroBenchmark
public static void calculateArraySummary(String[] args) {
// create a random data set
/* PROBLEM HERE:
* now I measure not only pool.invoke(finder) time,
* but also generateRandomArray method time
*/
final int[] array = generateRandomArray(1000000);
// submit the task to the pool
final ForkJoinPool pool = new ForkJoinPool(4);
final ArraySummator finder = new ArraySummator(array);
System.out.println(pool.invoke(finder));
}
private static int[] generateRandomArray(int length) {
final int[] array = new int[1000000];
final Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(100);
}
return array;
}
问题:我不希望我的程序考虑generateRandomArray
方法花费的时间.
Problem: I don't want my program take into account time spent by generateRandomArray
method.
问题: 如何从jmh测量中排除generateRandomArray
?
推荐答案
如有疑问,请查看JMH示例.通过某些夹具方法上的@Setup
注释可以实现所需的效果,该方法将在基准测试之前运行. JMH样本中的确切答案是
When in doubt, look through the JMH Samples. The effect you want is achieved by @Setup
annotation over some fixture method, which will be run before the benchmark. The exact answer in JMH Samples is here.
这篇关于JMH:不考虑内部方法时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文