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