-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathProgram.cs
51 lines (41 loc) · 1.34 KB
/
Program.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
48
49
50
51
using System.Numerics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<ArrayAdd>();
public class ArrayAdd
{
private static readonly int[] Numbers = Enumerable.Repeat(1, 512).ToArray();
[Benchmark(Baseline = true)]
public int GetSumNaive()
{
var result = 0;
for (var index = 0; index < Numbers.Length; index++)
{
var i = Numbers[index];
result += i;
}
return result;
}
[Benchmark]
public int SumLINQ() => Numbers.Sum();
[Benchmark]
public int SumPLINQ() => Numbers.AsParallel().Sum();
[Benchmark]
public int SIMDVectors()
{
// This depends on your hardware
// It basically says on how many entries can we performance this single operation
// aka how big is "multiple"
var vectorSize = Vector<int>.Count;
var accVector = Vector<int>.Zero;
// We are tiling the original list by the hardware vector size
for (var i = 0; i <= Numbers.Length - vectorSize; i += vectorSize)
{
var v = new Vector<int>(Numbers, i);
accVector = Vector.Add(accVector, v);
}
// Scalar-Product of our vector against the Unit vector is its sum
var result = Vector.Dot(accVector, Vector<int>.One);
return result;
}
}