Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created May 26, 2019 17:26
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 EgorBo/bbebdfee917e00575c43dafe12f7b5f0 to your computer and use it in GitHub Desktop.
Save EgorBo/bbebdfee917e00575c43dafe12f7b5f0 to your computer and use it in GitHub Desktop.
using System;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
class Program
{
static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
public class Case2_DivToMul
{
[Benchmark(Baseline = true)]
[Arguments(MathF.PI, 100)]
public float Old(float value, int iterations)
{
for (int i = 0; i < iterations; i++)
value = value / 10;
return value;
}
[Benchmark]
[Arguments(MathF.PI, 100)]
public float New(float value, int iterations)
{
for (int i = 0; i < iterations; i++)
value = value * 0.1f;
return value;
}
}
public class Case1_Fmadd
{
[Benchmark(Baseline = true)]
[Arguments(42.51f, 1/3f, 100)]
public float Old(float a, float b, int iterations)
{
float z = 1f;
for (int i = 0; i < iterations; i++)
z = z * a + b;
return z;
}
[Benchmark]
[Arguments(42.51f, 1 / 3f, 100)]
public float New(float a, float b, int iterations)
{
float z = 1f;
for (int i = 0; i < iterations; i++)
z = MathF.FusedMultiplyAdd(z, a, b); // let's pretend JIT generates it
return z;
}
}
public class Case5
{
[Benchmark(Baseline = true)]
[Arguments(42.51f, 1 / 3f, 100)]
public float Old(float a, float b, int iterations)
{
float z = 1f;
for (int i = 0; i < iterations; i++)
z = (z * a) + (z * b);
return z;
}
[Benchmark]
[Arguments(42.51f, 1 / 3f, 100)]
public float New(float a, float b, int iterations)
{
float z = 1f;
for (int i = 0; i < iterations; i++)
z = z * (a + b);
return z;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment