-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathWhere.cs
49 lines (41 loc) · 1.15 KB
/
Where.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
using System.Linq;
using BenchmarkDotNet.Attributes;
namespace StructLinq.Benchmark
{
[MemoryDiagnoser]
public class Where
{
private const int Count = 10000;
[Benchmark(Baseline = true)]
public int SysSelect()
{
return Enumerable.Range(0, Count).Where(x => x > 0).Sum();
}
[Benchmark]
public int DelegateSelect()
{
return StructEnumerable.Range(0, Count)
.Where(x => x > 0)
.Sum();
}
[Benchmark]
public int StructSelect()
{
var predicate = new WhereFunc();
return StructEnumerable.Range(0, Count).Where(ref predicate, x => x).Sum(x => x);
}
[Benchmark]
public int ConvertSelect()
{
var predicate = new WhereFunc();
return Enumerable.Range(0, Count).ToStructEnumerable().Where(ref predicate, x => x).Sum(x => x);
}
}
struct WhereFunc : IFunction<int, bool>
{
public readonly bool Eval(int element)
{
return element > 0;
}
}
}