-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAllOnArray.cs
More file actions
65 lines (55 loc) · 1.76 KB
/
AllOnArray.cs
File metadata and controls
65 lines (55 loc) · 1.76 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
using System.Linq;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using StructLinq.Array;
namespace StructLinq.Benchmark
{
[MemoryDiagnoser]
public class AllOnArray
{
private const int Count = 1000;
private readonly int[] array;
public AllOnArray()
{
array = Enumerable.ToArray(Enumerable.Range(0, Count));
}
[Benchmark]
public bool For()
{
var ints = array;
var arrayLength = ints.Length;
for (int i = 0; i < arrayLength; i++)
{
if (ints[i] >= arrayLength / 2)
return false;
}
return true;
}
[Benchmark(Baseline = true)]
public bool Linq() => array.All(x=> x < Count / 2);
[Benchmark]
public bool StructLinq() => array.ToStructEnumerable().All(x => x < Count /2);
[Benchmark]
public bool StructLinqZeroAlloc() => array.ToStructEnumerable().All(x => x < Count /2, x=>x );
[Benchmark]
public bool StructLinqIFunctionZeroAlloc()
{
var func = new AllFunction();
return array.ToStructEnumerable().All(ref func, x => x);
}
[Benchmark]
public bool StructLinqIFunctionZeroAllocOnStructEnumerable()
{
var func = new AllFunction();
return array.ToStructEnumerable().All(ref func, x => (IStructEnumerable<int, ArrayStructEnumerator<int>>)x);
}
private struct AllFunction : IFunction<int, bool>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Eval(int element)
{
return element < Count / 2;
}
}
}
}