JMH: don't take into account inner method time

走远了吗. 提交于 2020-01-04 23:25:17

问题


I have: Methods like this:

@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;
}

Problem: I don't want my program take into account time spent by generateRandomArray method.

Questuin: How can I exclude generateRandomArray from jmh measurements?


回答1:


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.



来源:https://stackoverflow.com/questions/23858961/jmh-dont-take-into-account-inner-method-time

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