Skip to content

Instantly share code, notes, and snippets.

@AndreyAkinshin
Created December 6, 2016 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreyAkinshin/3634f00bddcc3d339460feebac1bd0d0 to your computer and use it in GitHub Desktop.
Save AndreyAkinshin/3634f00bddcc3d339460feebac1bd0d0 to your computer and use it in GitHub Desktop.
DotNextHelsinki2016
using System;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Jobs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
namespace DotNextHelsinki
{
public class AllJitsJob : JobConfigBaseAttribute
{
public AllJitsJob() : base(Job.LegacyJitX86, Job.LegacyJitX64, Job.RyuJitX64, Job.Mono)
{
}
}
[AllJitsJob]
public class SumBench
{
const int N = 1024;
int[,] a = new int[N, N];
[Benchmark]
public double SumIJ()
{
var sum = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
sum += a[i, j];
return sum;
}
[Benchmark]
public double SumJI()
{
var sum = 0;
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
sum += a[i, j];
return sum;
}
}
[AllJitsJob]
public class BranchBench
{
const int N = 32767;
int[] sorted, unsorted; // random numbers [0..255]
public BranchBench()
{
unsorted = new int[N];
sorted = new int[N];
var rnd = new Random(42);
for (int i = 0; i < N; i++)
unsorted[i] = sorted[i] = rnd.Next(256);
Array.Sort(sorted);
}
private static int Sum(int[] data)
{
int sum = 0;
for (int i = 0; i < N; i++)
if (data[i] >= 128)
sum += data[i];
return sum;
}
[Benchmark]
public int Sorted() => Sum(sorted);
[Benchmark]
public int Unsorted() => Sum(unsorted);
}
[AllJitsJob]
public class InterfaceBench
{
private interface IInc
{
double Inc(double x);
}
private class Foo : IInc
{
public double Inc(double x) => x + 1;
}
private class Bar : IInc
{
public double Inc(double x) => x + 1;
}
private double Run(IInc inc)
{
double sum = 0;
for (int i = 0; i < 1001; i++)
sum += inc.Inc(0);
return sum;
}
[Benchmark]
public double FooFoo()
{
var foo1 = new Foo();
var foo2 = new Foo();
return Run(foo1) + Run(foo2);
}
[Benchmark]
public double FooBar()
{
var foo = new Foo();
var bar = new Bar();
return Run(foo) + Run(bar);
}
}
[AllJitsJob]
public class InliningBench
{
[Benchmark]
public int Calc() => WithoutStarg(0x11) + WithStarg(0x12);
public int WithoutStarg(int value) => value;
public int WithStarg(int value)
{
if (value < 0)
value = -value;
return value;
}
}
[LegacyJitX64Job, RyuJitX64Job]
public class SimdBench
{
private struct MyVector
{
public float X, Y, Z, W;
public MyVector(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MyVector operator *(MyVector left, MyVector right)
{
return new MyVector(left.X * right.X, left.Y * right.Y,
left.Z * right.Z, left.W * right.W);
}
}
private Vector4 vector1, vector2, vector3;
private MyVector myVector1, myVector2, myVector3;
[Benchmark]
public void MyMul() => myVector3 = myVector1 * myVector2;
[Benchmark]
public void BclMul() => vector3 = vector1 * vector2;
}
[RyuJitX64Job]
public class SquareRootsBench
{
[Benchmark]
public double Sqrt13() =>
Math.Sqrt(1) + Math.Sqrt(2) + Math.Sqrt(3) +
Math.Sqrt(4) + Math.Sqrt(5) + Math.Sqrt(6) +
Math.Sqrt(7) + Math.Sqrt(8) + Math.Sqrt(9) +
Math.Sqrt(10) + Math.Sqrt(11) + Math.Sqrt(12) +
+Math.Sqrt(13);
[Benchmark]
public double Sqrt14() =>
Math.Sqrt(1) + Math.Sqrt(2) + Math.Sqrt(3) +
Math.Sqrt(4) + Math.Sqrt(5) + Math.Sqrt(6) +
Math.Sqrt(7) + Math.Sqrt(8) + Math.Sqrt(9) +
Math.Sqrt(10) + Math.Sqrt(11) + Math.Sqrt(12) +
+Math.Sqrt(13) + Math.Sqrt(14);
}
internal class Program
{
public static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).RunAll();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment