-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortReversedBenchmark.java
122 lines (103 loc) · 4.18 KB
/
SortReversedBenchmark.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.blankspace.sort;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.profile.ClassloaderProfiler;
import org.openjdk.jmh.profile.CompilerProfiler;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.profile.StackProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import com.blankspace.sort.util.DataGenerator;
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class SortReversedBenchmark {
private int[] records;
@Setup(value = Level.Trial)
public void setUpInvocation() {
this.records = DataGenerator.getReversedRecords();
}
@Benchmark
public void measureBubbleSort(Blackhole blackhole) {
int[] bubbleRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new BubbleSort().sort(bubbleRecords));
}
@Benchmark
public void measureCocktailSort(Blackhole blackhole) {
int[] cocktailRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new CocktailSort().sort(cocktailRecords));
}
@Benchmark
public void measureHeapSort(Blackhole blackhole) {
int[] heapRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new HeapSort().sort(heapRecords));
}
@Benchmark
public void measureInsertSort(Blackhole blackhole) {
int[] insertRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new InsertSort().sort(insertRecords));
}
@Benchmark
public void measureMergeSortIteration(Blackhole blackhole) {
int[] mergeRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new MergeSortIteration().sort(mergeRecords));
}
@Benchmark
public void measureMergeSortRecursion(Blackhole blackhole) {
int[] mergeRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new MergeSortRecursion().sort(mergeRecords));
}
@Benchmark
public void measureQuickSortIteration(Blackhole blackhole) {
int[] quickRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new QuickSortIteration().sort(quickRecords));
}
@Benchmark
public void measureQuickSortRecursion(Blackhole blackhole) {
int[] quickRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new QuickSortRecursion().sort(quickRecords));
}
@Benchmark
public void measureSelectSort(Blackhole blackhole) {
int[] selectRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new SelectSort().sort(selectRecords));
}
@Benchmark
public void measureShellSort(Blackhole blackhole) {
int[] shellRecords = Arrays.copyOf(records, records.length);
blackhole.consume(new ShellSort().sort(shellRecords));
}
@Benchmark
public void measureJavaDefaultSort(Blackhole blackhole) {
int[] defaultRecords = Arrays.copyOf(records, records.length);
Arrays.sort(defaultRecords);
blackhole.consume(defaultRecords);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(SortReversedBenchmark.class.getSimpleName())
.addProfiler(StackProfiler.class)
.addProfiler(GCProfiler.class)
.addProfiler(ClassloaderProfiler.class)
.addProfiler(CompilerProfiler.class)
.build();
new Runner(opt).run();
}
}