Created
May 26, 2019 17:26
-
-
Save EgorBo/bbebdfee917e00575c43dafe12f7b5f0 to your computer and use it in GitHub Desktop.
This file contains 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
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