JMH:不考虑内部方法时间

时间:2014-05-25 19:10:31

标签: java benchmarking microbenchmark 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方法花费的时间。

Questuin: 如何从jmh测量中排除generateRandomArray

1 个答案:

答案 0 :(得分:11)

如有疑问,请查看JMH样本。您想要的效果是通过某些fixture方法的@Setup注释实现的,该方法将在基准测试之前运行。 JMH样本中的确切答案是here