Java JMH微基准递归快速排序
您好,我正在尝试对各种排序算法进行微基准测试,我发现jmh和基准测试quicksort存在一个奇怪的问题。也许我的实现有问题。如果有人能帮我看看问题出在哪里,我会很感兴趣的。首先,我将ubuntu 14.04与jdk 7和jmh 0.9.1结合使用。 以下是我如何尝试进行基准测试:Java JMH微基准递归快速排序,java,benchmarking,quicksort,microbenchmark,jmh,Java,Benchmarking,Quicksort,Microbenchmark,Jmh,您好,我正在尝试对各种排序算法进行微基准测试,我发现jmh和基准测试quicksort存在一个奇怪的问题。也许我的实现有问题。如果有人能帮我看看问题出在哪里,我会很感兴趣的。首先,我将ubuntu 14.04与jdk 7和jmh 0.9.1结合使用。 以下是我如何尝试进行基准测试: @OutputTimeUnit(TimeUnit.MILLISECONDS) @BenchmarkMode(Mode.AverageTime) @Warmup(iterations = 3, time = 1) @M
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 3, time = 1)
@State(Scope.Thread)
public class SortingBenchmark {
private int length = 100000;
private Distribution distribution = Distribution.RANDOM;
private int[] array;
int i = 1;
@Setup(Level.Iteration)
public void setUp() {
array = distribution.create(length);
}
@Benchmark
public int timeQuickSort() {
int[] sorted = Sorter.quickSort(array);
return sorted[i];
}
@Benchmark
public int timeJDKSort() {
Arrays.sort(array);
return array[i];
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(".*" + SortingBenchmark.class.getSimpleName() + ".*").forks(1)
.build();
new Runner(opt).run();
}
}
还有其他的算法,但我把它们遗漏了,因为它们或多或少是好的。由于某些原因,现在快速排序速度非常慢。时间的大小变慢了!更重要的是,我需要分配更多的堆栈空间,以便它在没有StackOverflowException的情况下运行。出于某种原因,quicksort似乎只执行大量递归调用。有趣的是,当我在我的主类中简单地运行算法时,它运行得很好(具有相同的随机分布和100000个元素)。不需要增加堆栈,简单的nanotime基准测试显示的时间与其他算法非常接近。在基准测试中,当使用jmh进行测试时,JDK排序速度非常快,并且与其他使用naive nanotime基准测试的算法更加一致。我是做错了什么还是错过了什么?
以下是我的快速排序算法:
public static int[] quickSort(int[] data) {
Sorter.quickSort(data, 0, data.length - 1);
return data;
}
private static void quickSort(int[] data, int sublistFirstIndex, int sublistLastIndex) {
if (sublistFirstIndex < sublistLastIndex) {
// move smaller elements before pivot and larger after
int pivotIndex = partition(data, sublistFirstIndex, sublistLastIndex);
// apply recursively to sub lists
Sorter.quickSort(data, sublistFirstIndex, pivotIndex - 1);
Sorter.quickSort(data, pivotIndex + 1, sublistLastIndex);
}
}
private static int partition(int[] data, int sublistFirstIndex, int sublistLastIndex) {
int pivotElement = data[sublistLastIndex];
int pivotIndex = sublistFirstIndex - 1;
for (int i = sublistFirstIndex; i < sublistLastIndex; i++) {
if (data[i] <= pivotElement) {
pivotIndex++;
ArrayUtils.swap(data, pivotIndex, i);
}
}
ArrayUtils.swap(data, pivotIndex + 1, sublistLastIndex);
return pivotIndex + 1; // return index of pivot element
}
公共静态int[]快速排序(int[]数据){
快速排序(数据,0,数据长度-1);
返回数据;
}
私有静态void快速排序(int[]数据,int sublistFirstIndex,int sublistLastIndex){
if(sublistFirstIndex 如果(data[i]没问题,因为这里真的应该有答案(而不必看问题下面的评论),我就把它放在这里
JMH中的迭代是一批基准方法调用(取决于迭代设置的时间)。因此,使用@Setup(Level.iteration)只会在调用序列的开头进行设置。由于数组在第一次调用后排序,所以在最坏的情况下会调用quicksort(一个排序数组),这就是为什么它需要这么长时间或破坏堆栈的原因
因此,解决方案是使用@Setup(Level.Invocation)
**
* Invocation level: to be executed for each benchmark method execution.
*
* <p><b>WARNING: HERE BE DRAGONS! THIS IS A SHARP TOOL.
* MAKE SURE YOU UNDERSTAND THE REASONING AND THE IMPLICATIONS
* OF THE WARNINGS BELOW BEFORE EVEN CONSIDERING USING THIS LEVEL.</b></p>
*
* <p>This level is only usable for benchmarks taking more than a millisecond
* per single {@link Benchmark} method invocation. It is a good idea to validate
* the impact for your case on ad-hoc basis as well.</p>
*
* <p>WARNING #1: Since we have to subtract the setup/teardown costs from
* the benchmark time, on this level, we have to timestamp *each* benchmark
* invocation. If the benchmarked method is small, then we saturate the
* system with timestamp requests, which introduce artificial latency,
* throughput, and scalability bottlenecks.</p>
*
* <p>WARNING #2: Since we measure individual invocation timings with this
* level, we probably set ourselves up for (coordinated) omission. That means
* the hiccups in measurement can be hidden from timing measurement, and
* can introduce surprising results. For example, when we use timings to
* understand the benchmark throughput, the omitted timing measurement will
* result in lower aggregate time, and fictionally *larger* throughput.</p>
*
* <p>WARNING #3: In order to maintain the same sharing behavior as other
* Levels, we sometimes have to synchronize (arbitrage) the access to
* {@link State} objects. Other levels do this outside the measurement,
* but at this level, we have to synchronize on *critical path*, further
* offsetting the measurement.</p>
*
* <p>WARNING #4: Current implementation allows the helper method execution
* at this Level to overlap with the benchmark invocation itself in order
* to simplify arbitrage. That matters in multi-threaded benchmarks, when
* one worker thread executing {@link Benchmark} method may observe other
* worker thread already calling {@link TearDown} for the same object.</p>
*/
**
*调用级别:为每个基准方法执行执行。
*
*警告:这里有龙!这是一个锋利的工具。
*确保你理解其中的道理和含义
*在考虑使用此级别之前,请查看下面的警告
*
*此级别仅适用于超过一毫秒的基准测试
*每个{@link Benchmark}方法调用。验证
*对您的案例的影响也是临时性的
*
*警告#1:因为我们必须从中减去安装/拆卸成本
*基准时间,在这个级别上,我们必须给每个基准打上时间戳
*如果基准方法很小,那么我们会使
*带有时间戳请求的系统,这会引入人为延迟,
*吞吐量和可扩展性瓶颈
*
*警告#2:因为我们用这个
*在这个层次上,我们可能设置了(协调的)遗漏。这意味着
*测量中的故障可以从定时测量中隐藏,并且
*可以带来令人惊讶的结果。例如,当我们使用计时来
*了解基准吞吐量,省略的定时测量将
*导致较低的聚合时间和虚构的较大吞吐量
*
*警告#3:为了保持与其他人相同的共享行为
*级别,我们有时必须同步(套利)访问
*{@link State}对象。其他级别在度量之外执行此操作,
*但在这个级别上,我们必须在*关键路径*上进行同步,更进一步
*抵消测量值
*
*警告#4:当前实现允许执行helper方法
*在这个级别上,可以按顺序与基准调用本身重叠
*简化套利。这在多线程基准测试中很重要,当
*一个执行{@link Benchmark}方法的工作线程可能会观察到另一个
*工作线程已为同一对象调用{@link TearDown}
*/
因此,正如Aleksey Shipilev所建议的,将阵列复制成本吸收到每个基准方法中。因为您正在比较相对性能,所以这不应该影响您的结果。至少,Arrays.sort()是就地排序,您仅在@Benchmark的第一次调用时进行排序。所有后续调用都在一个排序的数组上操作。是否每次在@Benchmark中都从源位置执行array.copyOf()?否,但我会在setup方法中为每次迭代创建新数组。迭代是单个@Benchmark调用的顺序。@setup(调用)会扭曲结果,因此更明智的选择是将复制成本降低到@Benchmark中。迭代是定时的,正如您从输出中看到的——因此迭代中发生的调用次数取决于调用持续时间。好的,谢谢!因为我正在相互比较算法,所以数组复制将在所有基准中执行。无论如何,这是正确的如果jmh有办法在每次调用时重新创建可变参数,那就太好了。非常感谢您对Aleksey的帮助!