-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathArrayOfIntSum.cs
47 lines (41 loc) · 1.33 KB
/
ArrayOfIntSum.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
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using StructLinq.Array;
using Enumerable = System.Linq.Enumerable;
namespace StructLinq.Benchmark
{
[MemoryDiagnoser]
public class ArrayOfIntSum
{
private readonly IEnumerable<int> sysArray;
private readonly int Count = 1_000;
private readonly int[] array;
public ArrayOfIntSum()
{
sysArray = Enumerable.ToArray(Enumerable.Range(0,Count));
array = Enumerable.ToArray(Enumerable.Range(0, Count));
}
[Benchmark]
public int Handmaded()
{
int sum = 0;
var enumerable = array;
foreach (var i in enumerable)
{
sum += enumerable[i];
}
return sum;
}
[Benchmark]
public int EnumerableLINQ() => sysArray.Sum();
[Benchmark(Baseline = true)]
public int ArrayLINQ() => array.Sum();
[Benchmark]
public int StructLinqZeroAlloc() => array.ToStructEnumerable().Sum(x=>x);
[Benchmark]
public int StructLinq() => array.ToStructEnumerable().Sum();
[Benchmark]
public int StructLinqZeroAllocWithVisitor() => array.ToStructEnumerable().Sum(x=> (IStructEnumerable<int, ArrayStructEnumerator<int>>)x);
}
}