-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathArrayWhereSelectSum.cs
More file actions
160 lines (135 loc) · 3.27 KB
/
ArrayWhereSelectSum.cs
File metadata and controls
160 lines (135 loc) · 3.27 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
// LinqGen.Benchmarks, Maxwell Keonwoo Kang <code.athei@gmail.com>, 2022
using BenchmarkDotNet.Attributes;
using StructLinq;
using Cathei.LinqGen;
using NetFabric.Hyperlinq;
namespace Cathei.LinqGen.Benchmarks.Cases;
[MemoryDiagnoser]
public class ArrayWhereSelectSum
{
private int[] TestData { get; set; } = null!;
[Params(100, 10_000, 1_000_000)]
public int Count { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
TestData = Utils.Generate(42, Count).ToArray();
}
[Benchmark]
public double ForLoop()
{
double sum = 0;
for (int i = 0; i < Count; ++i)
{
int value = TestData[i];
if (value % 2 == 0)
sum += value * 2.0;
}
return sum;
}
[Benchmark]
public double ForEachLoop()
{
double sum = 0;
foreach (var value in TestData)
{
if (value % 2 == 0)
sum += value * 2.0;
}
return sum;
}
[Benchmark(Baseline = true)]
public double Linq()
{
return TestData
.Where(x => x % 2 == 0)
.Select(x=> x * 2.0)
.Sum();
}
[Benchmark]
public double LinqGenDelegate()
{
return TestData
.Gen()
.Where(x => x % 2 == 0)
.Select(x => x * 2.0)
.Sum();
}
[Benchmark]
public double LinqGenStruct()
{
var predicate = new Predicate();
var selector = new Selector();
return TestData
.Gen()
.Where(predicate)
.Select(selector)
.Sum();
}
[Benchmark]
public double StructLinqDelegate()
{
return TestData
.ToStructEnumerable()
.Where(x => x % 2 == 0)
.Select(x => x * 2.0)
.Sum();
}
[Benchmark]
public double StructLinqStruct()
{
var predicate = new Predicate();
var selector = new Selector();
return TestData
.ToStructEnumerable()
.Where(ref predicate, x => x)
.Select(ref selector, x => x, x => x)
.Sum(x=> x);
}
[Benchmark]
public double HyperLinqDelegate()
{
return TestData
.AsValueEnumerable()
.Where(x => x % 2 == 0)
.Select(x => x * 2.0)
.Sum();
}
[Benchmark]
public double HyperLinqStruct()
{
return TestData
.AsValueEnumerable()
.Where<Predicate>()
.Select<double, Selector>()
.Sum();
}
readonly struct Predicate :
StructLinq.IFunction<int, bool>,
NetFabric.Hyperlinq.IFunction<int, bool>,
IStructFunction<int, bool>
{
public bool Eval(int element)
{
return element % 2 == 0;
}
public bool Invoke(int arg)
{
return arg % 2 == 0;
}
}
readonly struct Selector :
StructLinq.IFunction<int, double>,
NetFabric.Hyperlinq.IFunction<int, double>,
IStructFunction<int, double>
{
public double Eval(int element)
{
return element * 2.0;
}
public double Invoke(int arg)
{
return arg * 2.0;
}
}
}