forked from NetFabric/LinqBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayValueTypeSelectSum.cs
73 lines (64 loc) · 1.89 KB
/
ArrayValueTypeSelectSum.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
using BenchmarkDotNet.Attributes;
using JM.LinqFaster;
using NetFabric.Hyperlinq;
using StructLinq;
namespace LinqBenchmarks.Array.ValueType
{
public class ArrayValueTypeSelectSum: ValueTypeArrayBenchmarkBase
{
[Benchmark(Baseline = true)]
public int ForLoop()
{
var sum = 0;
var array = source;
for (var index = 0; index < array.Length; index++)
{
ref readonly var item = ref array[index];
sum += item.Value0;
}
return sum;
}
[Benchmark]
public int ForeachLoop()
{
var sum = 0;
foreach (var item in source)
{
sum += item.Value0;
}
return sum;
}
[Benchmark]
public int Linq()
=> System.Linq.Enumerable.Sum(source, item => item.Value0);
[Benchmark]
public int LinqFaster()
=> source.SumF(item => item.Value0);
[Benchmark]
public int LinqAF()
=> global::LinqAF.ArrayExtensionMethods.Sum(source, item => item.Value0);
[Benchmark]
public int StructLinq()
=> source
.ToRefStructEnumerable()
.Sum((in FatValueType item) => item.Value0);
[Benchmark]
public int StructLinq_IFunction()
{
var selector = new Value0Selector();
return source
.ToRefStructEnumerable()
.Sum(ref selector, x => x, x => x);
}
[Benchmark]
public int Hyperlinq()
=> source.AsValueEnumerable()
.Select(item => item.Value0)
.Sum();
[Benchmark]
public int Hyperlinq_IFunction()
=> source.AsValueEnumerable()
.Select<int, Value0Selector>()
.Sum();
}
}