forked from NetFabric/LinqBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayValueTypeWhereCount.cs
81 lines (71 loc) · 2.15 KB
/
ArrayValueTypeWhereCount.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
using BenchmarkDotNet.Attributes;
using com.tinyield;
using JM.LinqFaster;
using NetFabric.Hyperlinq;
using StructLinq;
namespace LinqBenchmarks.Array.ValueType
{
public class ArrayValueTypeWhereCount: ValueTypeArrayBenchmarkBase
{
[Benchmark(Baseline = true)]
public int ForLoop()
{
var count = 0;
var array = source;
for (var index = 0; index < array.Length; index++)
{
ref readonly var item = ref array[index];
if (item.IsEven())
count++;
}
return count;
}
[Benchmark]
public int ForeachLoop()
{
var count = 0;
foreach (var item in source)
{
if (item.IsEven())
count++;
}
return count;
}
[Benchmark]
public int Linq()
=> System.Linq.Enumerable.Count(source, item => item.IsEven());
[Benchmark]
public int LinqFaster()
=> source.CountF(item => item.IsEven());
[Benchmark]
public int LinqAF()
=> global::LinqAF.ArrayExtensionMethods.Count(source, item => item.IsEven());
[Benchmark]
public int StructLinq()
=> source
.ToRefStructEnumerable()
.Where((in FatValueType item) => item.IsEven())
.Count();
[Benchmark]
public int StructLinq_IFunction()
{
var predicate = new FatValueTypeIsEven();
return source
.ToRefStructEnumerable()
.Where(ref predicate, x => x)
.Count(x=> x);
}
[Benchmark]
public int Hyperlinq()
=> source.AsValueEnumerable()
.Where(item => item.IsEven())
.Count();
[Benchmark]
public int Hyperlinq_IFunction()
=> source.AsValueEnumerable()
.Where<FatValueTypeIsEven>()
.Count();
[Benchmark]
public long Tinyield() => Query.Of(source).Filter(i => i.IsEven()).Count();
}
}