-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSumOnSelectMany.cs
70 lines (59 loc) · 1.85 KB
/
SumOnSelectMany.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
using System.Linq;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using StructLinq.Array;
namespace StructLinq.Benchmark
{
[MemoryDiagnoser]
public class SumOnSelectMany
{
private int[][] array;
private const int Count = 1000;
public SumOnSelectMany()
{
array = Enumerable.Range(0, Count)
.Select(x => Enumerable.Range(0, x).ToArray())
.ToArray();
}
[Benchmark(Baseline = true)]
public int LINQ()
{
return array.SelectMany(x => x).Sum();
}
[Benchmark]
public int StructLINQ()
{
return array.ToStructEnumerable().SelectMany(x => x).Sum();
}
[Benchmark]
public int StructLINQWhereReturnIsStructEnumerable()
{
return array.ToStructEnumerable().SelectMany(x => x.ToStructEnumerable(), _ => _, _ => _).Sum(x => x);
}
[Benchmark]
public int StructLINQWithFunction()
{
var func = new SelectManyFunction();
return array.ToStructEnumerable().SelectMany(func, x => x, x => x, x => x).Sum(x => x);
}
[Benchmark]
public int StructLINQWithFunctionWithForeach()
{
var sum = 0;
var func = new SelectManyFunction();
foreach (var i in array.ToStructEnumerable().SelectMany(func, x=>x, x=> x, x=> x))
{
sum += i;
}
return sum;
}
internal struct SelectManyFunction : IFunction<int[], ArrayEnumerable<int>>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ArrayEnumerable<int> Eval(int[] element)
{
return element.ToStructEnumerable();
}
}
}
}