forked from NetFabric/LinqBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayValueTypeContains.cs
65 lines (56 loc) · 1.63 KB
/
ArrayValueTypeContains.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
using BenchmarkDotNet.Attributes;
using JM.LinqFaster;
using NetFabric.Hyperlinq;
using StructLinq;
namespace LinqBenchmarks.Array.ValueType
{
public class ArrayValueTypeContains: ValueTypeArrayBenchmarkBase
{
FatValueType value = new FatValueType(int.MaxValue);
[Benchmark(Baseline = true)]
public bool ForLoop()
{
var array = source;
for (var index = 0; index < array.Length; index++)
{
ref readonly var item = ref array[index];
if (item == value)
return true;
}
return false;
}
[Benchmark]
public bool ForeachLoop()
{
foreach (var item in source)
{
if (item == value)
return true;
}
return false;
}
[Benchmark]
public bool Linq()
=> System.Linq.Enumerable.Contains(source, value);
[Benchmark]
public bool LinqFaster()
=> source.ContainsF(value);
[Benchmark]
public bool LinqAF()
=> global::LinqAF.ArrayExtensionMethods.Contains(source, value);
[Benchmark]
public bool StructLinq()
=> source
.ToRefStructEnumerable()
.Contains(value);
[Benchmark]
public bool StructLinq_IFunction()
=> source
.ToRefStructEnumerable()
.Contains(value, x => x);
[Benchmark]
public bool Hyperlinq()
=> source.AsValueEnumerable()
.Contains(value);
}
}