/
IntroHardwareCounters.cs
57 lines (49 loc) · 1.46 KB
/
IntroHardwareCounters.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
52
53
54
55
56
57
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
namespace BenchmarkDotNet.Samples
{
[HardwareCounters(
HardwareCounter.BranchMispredictions,
HardwareCounter.BranchInstructions)]
public class IntroHardwareCounters
{
private const int N = 32767;
private readonly int[] sorted, unsorted;
public IntroHardwareCounters()
{
var random = new Random(0);
unsorted = new int[N];
sorted = new int[N];
for (int i = 0; i < N; i++)
sorted[i] = unsorted[i] = random.Next(256);
Array.Sort(sorted);
}
private static int Branch(int[] data)
{
int sum = 0;
for (int i = 0; i < N; i++)
if (data[i] >= 128)
sum += data[i];
return sum;
}
private static int Branchless(int[] data)
{
int sum = 0;
for (int i = 0; i < N; i++)
{
int t = (data[i] - 128) >> 31;
sum += ~t & data[i];
}
return sum;
}
[Benchmark]
public int SortedBranch() => Branch(sorted);
[Benchmark]
public int UnsortedBranch() => Branch(unsorted);
[Benchmark]
public int SortedBranchless() => Branchless(sorted);
[Benchmark]
public int UnsortedBranchless() => Branchless(unsorted);
}
}