Skip to content

Commit 3ef70fa

Browse files
committed
Added branch prediction
1 parent be7b470 commit 3ef70fa

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
12+
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.1" />
13+
</ItemGroup>
14+
15+
</Project>

BranchPrediction/BranchPrediction.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BranchPrediction", "BranchPrediction.csproj", "{3EE37DE4-9EBC-4E5C-8B21-CB6E57DDB499}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{3EE37DE4-9EBC-4E5C-8B21-CB6E57DDB499}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{3EE37DE4-9EBC-4E5C-8B21-CB6E57DDB499}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{3EE37DE4-9EBC-4E5C-8B21-CB6E57DDB499}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{3EE37DE4-9EBC-4E5C-8B21-CB6E57DDB499}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

BranchPrediction/Program.cs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+

2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Diagnosers;
4+
using BenchmarkDotNet.Running;
5+
6+
BenchmarkRunner.Run<Benchi>();
7+
8+
[HardwareCounters(HardwareCounter.BranchMispredictions)]
9+
public class Benchi
10+
{
11+
private const int ArraySize = 65535;
12+
private int[] unsortedArray = new int[ArraySize];
13+
private int[] sortedArray = new int[ArraySize];
14+
15+
public Benchi()
16+
{
17+
var random = new Random();
18+
for (var i = 0; i < ArraySize; i++)
19+
{
20+
unsortedArray[i] = sortedArray[i] = random.Next(256);
21+
}
22+
23+
Array.Sort(sortedArray);
24+
}
25+
26+
[Benchmark(Baseline = true)]
27+
public int Unsorted() => SumOfEverythingAbove128Unsorted(unsortedArray);
28+
29+
[Benchmark]
30+
public int Sorted() => SumOfEverythingAbove128Unsorted(sortedArray);
31+
32+
private static int SumOfEverythingAbove128Unsorted(int[] data)
33+
{
34+
var sum = 0;
35+
for (var i = 0; i < data.Length; i++)
36+
{
37+
if (data[i] >= 128)
38+
{
39+
sum += data[i];
40+
}
41+
}
42+
43+
return sum;
44+
}
45+
}

BranchPrediction/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Branch prediction - cost of an if
2+
3+
The blog post shows how branch prediction impacts the performance of your if statements
4+
5+
Found [here](https://steven-giesel.com/blogPost/0c84f371-08e3-4a57-bdd7-8b08cb308232)

EnumEqualsPerformance/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
The blog post shows how different enum comparison effect the performance and why
44

5-
* Found [here](https://steven-giesel.com/blogPost/0a8eec1a-84a2-4c2e-a083-8c7ccb372a41)
5+
Found [here](https://steven-giesel.com/blogPost/0a8eec1a-84a2-4c2e-a083-8c7ccb372a41)

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# BlogExamples
1+
# Blog - Examples
22

33
Contains all of my examples from various blog posts. You can find a comprehensive overview of all repositories here and where I used them in my blog:
44

55
| BlogPost | Publish Date |
66
| ---------------------------------------------------------- | ------------ |
7-
| [Enum.Equals Performance Analysis](EnumEqualsPerformance/) | 11.01.2022 |
7+
| [Enum.Equals Performance Analysis](EnumEqualsPerformance/) | 11.01.2022 |
8+
| [Branch Prediction](BranchPrediction/) | 09.11.2021 |

0 commit comments

Comments
 (0)