-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathElementAtOnArray.cs
38 lines (29 loc) · 1.12 KB
/
ElementAtOnArray.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
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using StructLinq.Array;
namespace StructLinq.Benchmark
{
[MemoryDiagnoser]
public class ElementAtOnArray
{
private const int Count = 1000;
private readonly int[] array;
private readonly IEnumerable<int> enumerable;
public ElementAtOnArray()
{
array = Enumerable.ToArray(Enumerable.Range(0, Count));
enumerable = Enumerable.ToArray(Enumerable.Range(0, Count));
}
[Benchmark(Baseline = true)]
public int Linq() => array.ElementAt(Count / 2);
[Benchmark]
public int EnumerableLinq() => enumerable.ElementAt(Count / 2);
[Benchmark]
public int StructLinq() => array.ToStructEnumerable().ElementAt(Count / 2);
[Benchmark]
public int StructLinqZeroAlloc() => array.ToStructEnumerable().ElementAt(Count / 2, x=>x);
[Benchmark]
public int StructLinqZeroAllocWithEnumerator() => array.ToStructEnumerable().ElementAt(Count / 2, x => (IStructEnumerable<int, ArrayStructEnumerator<int>>)x);
}
}