Last active
March 1, 2024 09:33
-
-
Save habbes/39340ad48cf0e648f496510da9fcfb5e to your computer and use it in GitHub Desktop.
sharpbench-benchmark-demo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// visit https://benchmarkdotnet.org/ for more info on BenchmarkDotNet | |
using System.Numerics; | |
using System.Runtime.CompilerServices; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Diagnosers; | |
[CategoriesColumn] | |
public class Benchmarks | |
{ | |
private int[]? A; | |
private int[]? B; | |
private int[]? C; | |
public int dataSize = 0x100000; | |
[GlobalSetup] | |
public void Setup() | |
{ | |
A = new int[dataSize]; | |
B = new int[dataSize]; | |
C = new int[dataSize]; | |
Random generator = new Random(1); | |
for (int i = 0; i < dataSize; i++) | |
{ | |
A[i] = generator.Next(0, 10); | |
B[i] = generator.Next(0, 10); | |
C[i] = 0; | |
} | |
} | |
[BenchmarkCategory("MemberWiseSum"), Benchmark] | |
public void MemberWiseSumScalar() => Compute.MemberWiseSumScalar(A, B, C); | |
[BenchmarkCategory("MemberWiseSum"), Benchmark] | |
public void MemberWiseSumSIMD() => Compute.MemberWiseSumSIMD(A, B, C); | |
[BenchmarkCategory("ArraySum"), Benchmark] | |
public void ArraySumSalar() => Compute.ArraySumScalar(A); | |
[BenchmarkCategory("ArraySum"), Benchmark] | |
public void ArraySumSIMD() => Compute.ArraySumSIMD(A); | |
} | |
public static class Compute | |
{ | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void MemberWiseSumScalar(int[] A, int[] B, int[] result) | |
{ | |
int size = A.Length; | |
for (int i = 0; i < size; i++) | |
{ | |
result[i] = A[i] + B[i]; | |
} | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void MemberWiseSumSIMD(int[] A, int[] B, int[] result) | |
{ | |
int size = A.Length; | |
for (int i = 0; i < size; i += Vector<int>.Count) | |
{ | |
Vector<int> v = new Vector<int>(A, i) + new Vector<int>(B, i); | |
v.CopyTo(result, i); | |
} | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static int ArraySumScalar(int[] A) | |
{ | |
int sum = 0; | |
foreach(int value in A) | |
{ | |
sum += value; | |
} | |
return sum; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static int ArraySumSIMD(int[] A) | |
{ | |
Vector<int> sums = Vector<int>.Zero; | |
for (int i = 0; i < A.Length; i += Vector<int>.Count) | |
{ | |
sums += new Vector<int>(A, i); | |
} | |
int finalSum = 0; | |
for (int n = 0; n < Vector<int>.Count; n++) | |
{ | |
finalSum += sums[n]; | |
} | |
return finalSum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment