-
Notifications
You must be signed in to change notification settings - Fork 21
/
Program.cs
45 lines (37 loc) · 1.05 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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Benchi>();
[HardwareCounters(HardwareCounter.BranchMispredictions)]
public class Benchi
{
private const int ArraySize = 65535;
private int[] unsortedArray = new int[ArraySize];
private int[] sortedArray = new int[ArraySize];
public Benchi()
{
var random = new Random();
for (var i = 0; i < ArraySize; i++)
{
unsortedArray[i] = sortedArray[i] = random.Next(256);
}
Array.Sort(sortedArray);
}
[Benchmark(Baseline = true)]
public int Unsorted() => SumOfEverythingAbove128Unsorted(unsortedArray);
[Benchmark]
public int Sorted() => SumOfEverythingAbove128Unsorted(sortedArray);
private static int SumOfEverythingAbove128Unsorted(int[] data)
{
var sum = 0;
for (var i = 0; i < data.Length; i++)
{
if (data[i] >= 128)
{
sum += data[i];
}
}
return sum;
}
}