-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathSortBenchmarks.cs
More file actions
177 lines (136 loc) · 3.78 KB
/
SortBenchmarks.cs
File metadata and controls
177 lines (136 loc) · 3.78 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
namespace Towel_Benchmarking;
[Tag(Program.Name, "Sorting Algorithms")]
[Tag(Program.OutputFile, nameof(SortBenchmarks))]
public class SortBenchmarks
{
[Params(10, 1000, 10000)]
public int N;
public int[]? Values;
[IterationSetup]
public void IterationSetup()
{
Values = (..N).ToArray();
Random random = new(7);
Shuffle<int>(Values, random);
}
[Benchmark]
public void SystemArraySort() => Array.Sort(Values!);
[Benchmark]
public void SystemArraySortDelegate() => Array.Sort(Values!, (int a, int b) => a.CompareTo(b));
[Benchmark]
public void SystemArraySortIComparer() => Array.Sort(Values!, default(ComparerInt));
[Benchmark]
public void BubbleRunTime() => SortBubble<int>(Values);
[Benchmark]
public void BubbleCompileTime() => SortBubble<int, Int32Compare>(Values);
[Benchmark]
public void SelectionRunTime() => SortSelection<int>(Values);
[Benchmark]
public void SelectionCompileTime() => SortSelection<int, Int32Compare>(Values);
[Benchmark]
public void InsertionRunTime() => SortInsertion<int>(Values);
[Benchmark]
public void InsertionCompileTime() => SortInsertion<int, Int32Compare>(Values);
[Benchmark]
public void QuickRunTime() => SortQuick<int>(Values);
[Benchmark]
public void QuickCompileTime() => SortQuick<int, Int32Compare>(Values);
[Benchmark]
public void MergeRunTime() => SortMerge<int>(Values);
[Benchmark]
public void MergeCompileTime() => SortMerge<int, Int32Compare>(Values);
[Benchmark]
public void HeapRunTime() => SortHeap<int>(Values);
[Benchmark]
public void HeapCompileTime() => SortHeap<int, Int32Compare>(Values);
[Benchmark]
public void IntroRunTime() => SortIntro<int>(Values);
[Benchmark]
public void IntroCompileTime() => SortIntro<int, Int32Compare>(Values);
[Benchmark]
public void OddEvenRunTime() => SortOddEven<int>(Values);
[Benchmark]
public void OddEvenCompileTime() => SortOddEven<int, Int32Compare>(Values);
[Benchmark]
public void GnomeRunTime() => SortGnome<int>(Values);
[Benchmark]
public void GnomeCompileTime() => SortGnome<int, Int32Compare>(Values);
[Benchmark]
public void CombRunTime() => SortComb<int>(Values);
[Benchmark]
public void CombCompileTime() => SortComb<int, Int32Compare>(Values);
[Benchmark]
public void ShellRunTime() => SortShell<int>(Values);
[Benchmark]
public void ShellCompileTime() => SortShell<int, Int32Compare>(Values);
[Benchmark]
public void CocktailRunTime() => SortCocktail<int>(Values);
[Benchmark]
public void CocktailCompileTime() => SortCocktail<int, Int32Compare>(Values);
[Benchmark]
public void CycleRunTime() => SortCycle<int>(Values);
[Benchmark]
public void CycleCompileTime() => SortCycle<int, Int32Compare>(Values);
[Benchmark]
public void PancakeRunTime() => SortPancake<int>(Values);
[Benchmark]
public void PancakeCompileTime() => SortPancake<int, Int32Compare>(Values);
[Benchmark]
public void StoogeRunTime()
{
if (N > 1000)
{
throw new Exception("Too Slow.");
}
SortStooge<int>(Values);
}
[Benchmark]
public void StoogeCompileTime()
{
if (N > 1000)
{
throw new Exception("Too Slow.");
}
SortStooge<int, Int32Compare>(Values);
}
[Benchmark]
public void SlowRunTime()
{
if (N > 10)
{
throw new Exception("Too Slow.");
}
SortSlow<int>(Values);
}
[Benchmark]
public void SlowCompileTime()
{
if (N > 10)
{
throw new Exception("Too Slow.");
}
SortSlow<int, Int32Compare>(Values);
}
[Benchmark]
public void BogoRunTime()
{
if (N > 10)
{
throw new Exception("Too Slow.");
}
SortBogo<int>(Values);
}
[Benchmark]
public void BogoCompileTime()
{
if (N > 10)
{
throw new Exception("Too Slow.");
}
SortBogo<int, Int32Compare>(Values);
}
public struct ComparerInt : System.Collections.Generic.IComparer<int>
{
public int Compare(int a, int b) => a.CompareTo(b);
}
}