-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathSpanVsArraySortingBenchmarks.cs
More file actions
68 lines (54 loc) · 2.3 KB
/
SpanVsArraySortingBenchmarks.cs
File metadata and controls
68 lines (54 loc) · 2.3 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
namespace Towel_Benchmarking;
[Tag(Program.Name, "Span vs Array Sorting")]
[Tag(Program.OutputFile, nameof(SpanVsArraySortingBenchmarks))]
public class SpanVsArraySortingBenchmarks
{
[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 ArrayBubbleRunTime() => SortBubbleArray(Values!);
[Benchmark]
public void ArrayBubbleCompileTime() => SortBubbleArray<int, Int32Compare>(Values!);
[Benchmark]
public void SpanBubbleRunTime() => SortBubble(Values.AsSpan());
[Benchmark]
public void SpanBubbleCompileTime() => SortBubble<int, Int32Compare>(Values.AsSpan());
public struct ComparerInt : System.Collections.Generic.IComparer<int>
{
public int Compare(int a, int b) => a.CompareTo(b);
}
/// <inheritdoc cref="XML_SortBubble"/>
public static void SortBubbleArray<T>(T[] array, Func<T, T, CompareResult>? compare = null) =>
SortBubbleArray(array, 0, array.Length - 1, compare);
/// <inheritdoc cref="XML_SortBubble"/>
public static void SortBubbleArray<T, TCompare>(T[] array, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult> =>
SortBubbleArray(array, 0, array.Length - 1, compare);
/// <inheritdoc cref="XML_SortBubble"/>
public static void SortBubbleArray<T>(T[] array, int start, int end, Func<T, T, CompareResult>? compare = null) =>
SortBubble<T, GetIndexArray<T>, SetIndexArray<T>, SFunc<T, T, CompareResult>>(start, end, array, array, compare ?? Compare);
/// <inheritdoc cref="XML_SortBubble"/>
public static void SortBubbleArray<T, TCompare>(T[] array, int start, int end, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult> =>
SortBubble<T, GetIndexArray<T>, SetIndexArray<T>, TCompare>(start, end, array, array, compare);
}
public struct GetIndexArray<T> : IFunc<int, T>
{
internal T[] Array;
public T Invoke(int index) => Array[index];
public static implicit operator GetIndexArray<T>(T[] array) => new() { Array = array, };
}
public struct SetIndexArray<T> : IAction<int, T>
{
internal T[] Array;
public void Invoke(int index, T value) => Array[index] = value;
public static implicit operator SetIndexArray<T>(T[] array) => new() { Array = array, };
}