-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathOrderByReference.cs
More file actions
134 lines (114 loc) · 3.15 KB
/
OrderByReference.cs
File metadata and controls
134 lines (114 loc) · 3.15 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
// LinqGen.Benchmarks, Maxwell Keonwoo Kang <code.athei@gmail.com>, 2022
using BenchmarkDotNet.Attributes;
using StructLinq;
namespace Cathei.LinqGen.Benchmarks.Cases;
[MemoryDiagnoser]
public class OrderByReference
{
public class IntWrapper : IComparable<IntWrapper>
{
public int inner;
public int CompareTo(IntWrapper? other)
{
return inner.CompareTo(other!.inner);
}
}
private static List<IntWrapper> smallList;
private static List<IntWrapper> mediumList;
private static List<IntWrapper> largeList;
static OrderByReference()
{
var rand = new Random(1024);
Fill(smallList = new(), 20, rand);
Fill(mediumList = new(), 500, rand);
Fill(largeList = new(), 10000, rand);
}
private static void Fill(List<IntWrapper> list, int count, Random random)
{
for (int i = 0; i < count; i++)
list.Add(new IntWrapper { inner = random.Next(1000) });
}
public static IEnumerable<IntWrapper[]> Lists
{
get
{
yield return smallList.ToArray();
yield return mediumList.ToArray();
yield return largeList.ToArray();
}
}
[Benchmark]
[ArgumentsSource(nameof(Lists))]
public double Linq(IntWrapper[] list)
{
return list.OrderBy(x => x)
.Sum(x => x.inner);
}
[Benchmark]
[ArgumentsSource(nameof(Lists))]
public double LinqGenDelegate(IntWrapper[] list)
{
return list.Gen()
.Order()
.Sum(x => x.inner);
}
[Benchmark]
[ArgumentsSource(nameof(Lists))]
public double LinqGenStruct(IntWrapper[] list)
{
return list.Gen()
.Order(new Comparer())
.Sum(new Selector());
}
[Benchmark]
[ArgumentsSource(nameof(Lists))]
public double StructLinqDelegate(IntWrapper[] list)
{
return list.ToStructEnumerable()
.Order()
.Sum(x => x.inner);
}
[Benchmark]
[ArgumentsSource(nameof(Lists))]
public double StructLinqStruct(IntWrapper[] list)
{
var comparer = new Comparer();
var selector = new Selector();
return list.ToStructEnumerable()
.Order(ref comparer, x => x)
.Sum(ref selector, x => x, x => x);
}
readonly struct Identity : IStructFunction<IntWrapper, IntWrapper>
{
public IntWrapper Invoke(IntWrapper arg)
{
return arg;
}
}
readonly struct Selector :
StructLinq.IFunction<IntWrapper, int>,
IStructFunction<IntWrapper, int>
{
public int Eval(IntWrapper element)
{
return element.inner;
}
public int Invoke(IntWrapper arg)
{
return arg.inner;
}
}
readonly struct Comparer :
IStructFunction<IntWrapper, IntWrapper, int>,
IComparer<IntWrapper>
{
public int Invoke(IntWrapper arg1, IntWrapper arg2)
{
return arg1.inner - arg2.inner;
}
public int Compare(IntWrapper? x, IntWrapper? y)
{
return x!.inner - y!.inner;
}
}
}