13

问题

在用 Java 对一个简单的 QuickSort 实现进行基准测试时,我在n vs time绘制的图形中遇到了意想不到的问题:

在此处输入图像描述

我知道 HotSpot 在某些方法似乎被大量使用后会尝试将代码编译为本机,因此我使用-XX:+PrintCompilation. 经过反复试验,它似乎总是以相同的方式编译算法的方法:

@ iteration 6 -> sorting.QuickSort::swap (15 bytes)
@ iteration 7 -> sorting.QuickSort::partition (66 bytes)
@ iteration 7 -> sorting.QuickSort::quickSort (29 bytes)

我用这个添加的信息重复上面的图形,只是为了让事情更清楚一点:

在此处输入图像描述

在这一点上,我们都必须问自己:为什么在编译代码之后我们仍然会遇到那些丑陋的驼峰?也许它与算法本身有关?当然可以,幸运的是,我们有一种快速的方法来解决这个问题,使用-XX:CompileThreshold=0

在此处输入图像描述

无赖!它确实必须是 JVM 在后台执行的操作。但是什么?我推测,虽然代码正在编译,但可能需要一段时间才能真正开始使用已编译的代码。也许在这里和那里添加几个Thread.sleep()s 可以帮助我们解决这个问题?

在此处输入图像描述

哎哟! 绿色函数是 QuickSort 的代码,每次运行之间的内部间隔为 1000 毫秒(详见附录),而蓝色函数是我们的旧函数(仅用于比较)。

首先,给 HotSpot 时间似乎只会让事情变得更糟!也许它看起来只是因为其他一些因素而变得更糟,比如缓存问题?

免责声明:我正在对所示图形的每个点进行 1000 次试验,并System.nanoTime()用于测量结果。

编辑

在这个阶段,你们中的一些人可能想知道使用sleep()可能会如何扭曲结果。我再次运行 Red Plot(没有本地编译),现在中间有睡眠:

在此处输入图像描述

可怕的!

附录

在这里我展示QuickSort我正在使用的代码,以防万一:

public class QuickSort {

    public <T extends Comparable<T>> void sort(int[] table) {
        quickSort(table, 0, table.length - 1);
    }

    private static <T extends Comparable<T>> void quickSort(int[] table,
            int first, int last) {
        if (first < last) { // There is data to be sorted.
            // Partition the table.
            int pivotIndex = partition(table, first, last);
            // Sort the left half.
            quickSort(table, first, pivotIndex - 1);
            // Sort the right half.
            quickSort(table, pivotIndex + 1, last);
        }
    }

    /**
     * @author http://en.wikipedia.org/wiki/Quick_Sort
     */
    private static <T extends Comparable<T>> int partition(int[] table,
            int first, int last) {
        int pivotIndex = (first + last) / 2;
        int pivotValue = table[pivotIndex];
        swap(table, pivotIndex, last);
        int storeIndex = first;
        for (int i = first; i < last; i++) {
            if (table[i]-(pivotValue) <= 0) {
                swap(table, i, storeIndex);
                storeIndex++;
            }
        }
        swap(table, storeIndex, last);
        return storeIndex;
    }

    private static <T> void swap(int[] a, int i, int j) {
        int h = a[i];
        a[i] = a[j];
        a[j] = h;
    }
}

以及我用来运行我的基准测试的代码:

public static void main(String[] args) throws InterruptedException, IOException {
    QuickSort quickSort = new QuickSort();

    int TRIALS = 1000;

    File file = new File(Long.toString(System.currentTimeMillis()));
    System.out.println("Saving @ \"" + file.getAbsolutePath() + "\"");

    for (int x = 0; x < 30; ++x) {
    //          if (x > 4 && x < 17)
    //              Thread.sleep(1000);

        int[] values = new int[x];

        long start = System.nanoTime();

        for (int i = 0; i < TRIALS; ++i)
            quickSort.sort(values);

        double duration = (System.nanoTime() - start) / TRIALS;
        String line = x + "\t" + duration;
        System.out.println(line);
        FileUtils.writeStringToFile(file, line + "\r\n", true);
    }
}
4

1 回答 1

5

好吧,看来我自己解决了这个问题。

我认为编译的代码可能需要一段时间才能启动的想法是正确的。问题是我实际实现基准测试代码的方式存在缺陷:

if (x > 4 && x < 17)
    Thread.sleep(1000);

在这里,我假设唯一的“受影响”区域在 4 到 17 之间,我可以继续对这些值进行睡眠。事实并非如此。下面的情节可能很有启发性:

在此处输入图像描述

在这里,我将原始的无编译功能(红色)与另一个无编译功能进行比较,但中间有睡眠。如您所见,它们以不同的数量级工作,这意味着混合使用和不使用睡眠的代码结果将产生不合理的结果,正如我所做的那样。

最初的问题仍未得到解答。是什么导致即使在编译发生后也会出现驼峰?让我们尝试找出这一点,在所有得分中加入 1s 睡眠:

在此处输入图像描述

这产生了预期的结果。奇怪的驼峰正在发生,本机代码仍然没有启动。

比较 sleep 50ms 和 sleep 1000ms 函数再次得出预期结果:

在此处输入图像描述

(灰色的似乎还是有点延迟)

于 2012-04-05T22:08:06.377 回答