-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFirstOnArray.cs
33 lines (26 loc) · 874 Bytes
/
FirstOnArray.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
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
namespace StructLinq.Benchmark
{
[MemoryDiagnoser]
public class FirstOnArray
{
private const int Count = 1000;
private readonly int[] array;
private readonly IEnumerable<int> enumerable;
public FirstOnArray()
{
array = Enumerable.ToArray(Enumerable.Range(0, Count));
enumerable = Enumerable.ToArray(Enumerable.Range(0, Count));
}
[Benchmark(Baseline = true)]
public int Linq() => array.First();
[Benchmark]
public int EnumerableLinq() => enumerable.First();
[Benchmark]
public int StructLinq() => array.ToStructEnumerable().First();
[Benchmark]
public int StructLinqZeroAlloc() => array.ToStructEnumerable().First(x=>x);
}
}