-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathListInt32WhereSelectToArray.cs
More file actions
109 lines (96 loc) · 2.93 KB
/
ListInt32WhereSelectToArray.cs
File metadata and controls
109 lines (96 loc) · 2.93 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
using System.Runtime.InteropServices;
using ListExtensions = Faslinq.ListExtensions;
namespace LinqBenchmarks.List.Int32;
public partial class ListInt32WhereSelectToArray: Int32ListBenchmarkBase
{
[Benchmark(Baseline = true)]
public int[] ForLoop()
{
var list = new List<int>();
for (var index = 0; index < source.Count; index++)
{
var item = source[index];
if (item.IsEven())
list.Add(item * 3);
}
return list.ToArray();
}
#pragma warning disable HLQ010 // Consider using a 'for' loop instead.
[Benchmark]
public int[] ForeachLoop()
{
var list = new List<int>();
foreach (var item in source)
{
if (item.IsEven())
list.Add(item * 3);
}
return list.ToArray();
}
#pragma warning restore HLQ010 // Consider using a 'for' loop instead.
[Benchmark]
public int[] Linq()
=> System.Linq.Enumerable
.Where(source, item => item.IsEven())
.Select(item => item * 3)
.ToArray();
[Benchmark]
public int[] LinqFaster()
=> source
.WhereSelectF(item => item.IsEven(), item => item * 3)
.ToArray();
[Benchmark]
public int[] LinqFasterer()
=> EnumerableF.ToArrayF(
EnumerableF.SelectF(
EnumerableF.WhereF(source, item => item.IsEven()),
item => item * 3)
);
[Benchmark]
public int[] LinqAF()
=> global::LinqAF.ListExtensionMethods.Where(source, item => item.IsEven()).Select(item => item * 3).ToArray();
#if DOTNET5_0_OR_GREATER
[Benchmark]
public int[] SpanLinq()
=> CollectionsMarshal.AsSpan(source)
.Where(item => item.IsEven())
.Select(item => item * 3)
.ToArray();
#endif
[Benchmark]
public int[] StructLinq()
=> source.ToStructEnumerable()
.Where(item => item.IsEven())
.Select(item => item * 3)
.ToArray();
[Benchmark]
public int[] StructLinq_ValueDelegate()
{
var predicate = new Int32IsEven();
var selector = new TripleOfInt32();
return source
.ToStructEnumerable()
.Where(ref predicate, x => x)
.Select(ref selector, x => x, x => x)
.ToArray(x => x);
}
[Benchmark]
public int[] Hyperlinq()
=> source.AsValueEnumerable()
.Where(item => item.IsEven())
.Select(item => item * 3)
.ToArray();
[Benchmark]
public int[] Hyperlinq_ValueDelegate()
=> source.AsValueEnumerable()
.Where<Int32IsEven>()
.Select<int, TripleOfInt32>()
.ToArray();
[Benchmark]
public int[] Faslinq()
=> ListExtensions.WhereSelect(
source,
item => item.IsEven(),
item => item * 3)
.ToArray();
}