-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathSorting.cs
178 lines (158 loc) · 5.25 KB
/
Sorting.cs
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
178
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace Span
{
[BenchmarkCategory(Categories.Runtime)]
[InvocationCount(InvocationsPerIteration)]
[MinWarmupCount(6, forceAutoWarmup: true)] // when InvocationCount is set, BDN does not run Pilot Stage, so to get the code promoted to Tier 1 before Actual Workload, we enforce more Warmups
public class Sorting
{
private const int InvocationsPerIteration = 10_000;
[Params(Utils.DefaultCollectionSize)]
public int Size;
private int _iterationIndex = 0;
private int[] _values;
private int[][] _arrays;
[GlobalSetup]
public void Setup() => _values = ValuesGenerator.ArrayOfUniqueValues<int>(Size);
[IterationSetup]
public void SetupIteration() => Utils.FillArrays(ref _arrays, InvocationsPerIteration, _values);
[IterationCleanup]
public void CleanupIteration() => _iterationIndex = 0; // after every iteration end we set the index to 0
[BenchmarkCategory(Categories.Span)]
[Benchmark]
public void QuickSortSpan() => TestQuickSortSpan(new Span<int>(_arrays[_iterationIndex++]));
[BenchmarkCategory(Categories.Span)]
[Benchmark]
public void BubbleSortSpan() => TestBubbleSortSpan(new Span<int>(_arrays[_iterationIndex++]));
[Benchmark]
public void QuickSortArray() => TestQuickSortArray(_arrays[_iterationIndex++], 0, Size - 1);
[Benchmark]
public void BubbleSortArray() => TestBubbleSortArray(_arrays[_iterationIndex++]);
[MethodImpl(MethodImplOptions.NoInlining)]
static void TestQuickSortArray(int[] data, int lo, int hi)
{
if (lo >= hi)
{
return;
}
int i, j;
int pivot, temp;
for (i = lo, j = hi, pivot = data[hi]; i < j;)
{
while (i < j && data[i] <= pivot)
{
++i;
}
while (j > i && data[j] >= pivot)
{
--j;
}
if (i < j)
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
if (i != hi)
{
temp = data[i];
data[i] = pivot;
data[hi] = temp;
}
TestQuickSortArray(data, lo, i - 1);
TestQuickSortArray(data, i + 1, hi);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void TestQuickSortSpan(Span<int> data)
{
if (data.Length <= 1)
{
return;
}
int lo = 0;
int hi = data.Length - 1;
int i, j;
int pivot, temp;
for (i = lo, j = hi, pivot = data[hi]; i < j;)
{
while (i < j && data[i] <= pivot)
{
++i;
}
while (j > i && data[j] >= pivot)
{
--j;
}
if (i < j)
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
if (i != hi)
{
temp = data[i];
data[i] = pivot;
data[hi] = temp;
}
TestQuickSortSpan(data.Slice(0, i));
TestQuickSortSpan(data.Slice(i + 1));
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void TestBubbleSortArray(int[] data)
{
bool swap;
int temp;
int n = data.Length - 1;
do
{
swap = false;
for (int i = 0; i < n; i++)
{
if (data[i] > data[i + 1])
{
temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
swap = true;
}
}
--n;
}
while (swap);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void TestBubbleSortSpan(Span<int> span)
{
bool swap;
int temp;
int n = span.Length - 1;
do
{
swap = false;
for (int i = 0; i < n; i++)
{
if (span[i] > span[i + 1])
{
temp = span[i];
span[i] = span[i + 1];
span[i + 1] = temp;
swap = true;
}
}
--n;
}
while (swap);
}
}
}