forked from NetFabric/LinqBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumerableInt32WhereSelectToArray.cs
75 lines (67 loc) · 2.16 KB
/
EnumerableInt32WhereSelectToArray.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
using BenchmarkDotNet.Attributes;
using com.tinyield;
using NetFabric.Hyperlinq;
using StructLinq;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
namespace LinqBenchmarks.Enumerable.Int32
{
public partial class EnumerableInt32WhereSelectToArray: EnumerableInt32BenchmarkBase
{
[Benchmark(Baseline = true)]
public int[] ForeachLoop()
{
var list = new List<int>();
foreach (var item in source)
{
if (item.IsEven())
list.Add(item * 2);
}
return list.ToArray();
}
[Benchmark]
public int[] Linq()
=> source.Where(item => item.IsEven()).Select(item => item * 2).ToArray();
[Benchmark]
public int[] LinqAF()
=> global::LinqAF.IEnumerableExtensionMethods
.Where(source, item => item.IsEven())
.Select(item => item * 2)
.ToArray();
[Benchmark]
public int[] StructLinq()
=> source.ToStructEnumerable()
.Where(item => item.IsEven())
.Select(item => item * 2)
.ToArray();
[Benchmark]
public int[] StructLinq_IFunction()
{
var predicate = new Int32IsEven();
var selector = new DoubleOfInt32();
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 * 2)
.ToArray();
[Benchmark]
public int[] Hyperlinq_IFunction()
=> source.AsValueEnumerable()
.Where<Int32IsEven>()
.Select<int, DoubleOfInt32>()
.ToArray();
[Benchmark]
public int[] Tinyield() => Query.FromEnumerable(source)
.Filter(i => i.IsEven())
.Map(i => i * 2)
.ToArray();
}
}