Hello, I am trying to perform micro-tests of various sorting algorithms, and I had a strange problem with jmh and quicksort benchmarking. Maybe something is wrong with my implementation. I would be interested if someone helps me see where the problem is. First of all, I am using ubuntu 14.04 with jdk 7 and jmh 0.9.1. Here is how I am trying to make a test:
@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();
}
}
, , . quicksort - . ! - StackOverflowException. , - quicksort . , - ( 100000 ). , , . JDK jmh . - - ?
:
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) {
int pivotIndex = partition(data, sublistFirstIndex, sublistLastIndex);
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;
}
, - (O (n ^ 2)), . , , , jmh . , - . : https://github.com/ignl/SortingAlgos/