-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathForEachOnListWithSelect.cs
More file actions
86 lines (76 loc) · 2.05 KB
/
ForEachOnListWithSelect.cs
File metadata and controls
86 lines (76 loc) · 2.05 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
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
namespace StructLinq.Benchmark
{
[MemoryDiagnoser]
public class ForEachOnListWithSelect
{
private readonly List<int> list;
public ForEachOnListWithSelect()
{
list = Enumerable.Range(-1, 10000).ToList();
}
[Benchmark(Baseline = true)]
public int LINQ()
{
var sum = 0;
foreach (var i in list.Select(x=> x * 2))
{
sum += i;
}
return sum;
}
[Benchmark]
public int StructLinqWithFunc()
{
var sum = 0;
foreach (var i in list.ToStructEnumerable().Select(x=> x * 2, x=>x))
{
sum += i;
}
return sum;
}
[Benchmark]
public int StructLinqWithFuncAsEnumerable()
{
var sum = 0;
foreach (var i in list.ToStructEnumerable().Select(x=> x * 2, x=>x).ToEnumerable())
{
sum += i;
}
return sum;
}
[Benchmark]
public int StructLinqWithStructFunc()
{
var sum = 0;
var func = new Mult2();
foreach (var i in list.ToStructEnumerable().Select(ref func, x=> x, x=> x))
{
sum += i;
}
return sum;
}
[Benchmark]
public int StructLinqWithStructFuncAsEnumerable()
{
var sum = 0;
var func = new Mult2();
foreach (var i in list.ToStructEnumerable().Select(ref func, x=> x, x=> x).ToEnumerable())
{
sum += i;
}
return sum;
}
public readonly struct Mult2 : IFunction<int, int>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Eval(int element)
{
return element * 2;
}
}
}
}