Created
February 11, 2019 18:23
-
-
Save HaloFour/5bf3f2f3a48183aeaab26a3c5aad25ab to your computer and use it in GitHub Desktop.
Reference Operators Crude Benchmark
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 BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System; | |
/* | |
BenchmarkDotNet=v0.11.3, OS=Windows 10.0.17763.55 (1809/October2018Update/Redstone5) | |
Intel Xeon CPU E5405 2.00GHz, 1 CPU, 4 logical and 4 physical cores | |
.NET Core SDK=2.1.500 | |
[Host] : .NET Core 2.1.6 (CoreCLR 4.6.27019.06, CoreFX 4.6.27019.05), 64bit RyuJIT | |
Core : .NET Core 2.1.6 (CoreCLR 4.6.27019.06, CoreFX 4.6.27019.05), 64bit RyuJIT | |
Job=Core Runtime=Core | |
Method | Mean | Error | StdDev | Ratio | | |
------------------- |----------:|----------:|----------:|------:| | |
Operators | 18.057 ns | 0.0515 ns | 0.0481 ns | 1.00 | | |
OutMethods | 6.872 ns | 0.0137 ns | 0.0129 ns | 0.38 | | |
InOutMethods | 6.166 ns | 0.0155 ns | 0.0145 ns | 0.34 | | |
RefReturnMethods | 6.470 ns | 0.0408 ns | 0.0382 ns | 0.36 | | |
InRefReturnMethods | 6.160 ns | 0.0063 ns | 0.0049 ns | 0.34 | | |
*/ | |
namespace Benchmarks | |
{ | |
public struct Vec3 | |
{ | |
public double X; | |
public double Y; | |
public double Z; | |
public Vec3(double x, double y, double z) | |
{ | |
this.X = x; | |
this.Y = y; | |
this.Z = z; | |
} | |
public static Vec3 operator +(Vec3 x, Vec3 y) | |
{ | |
x.X += y.X; | |
x.Y += y.X; | |
x.Z += y.Z; | |
return x; | |
} | |
public static void Add1(Vec3 x, Vec3 y, out Vec3 result) | |
{ | |
result.X = x.X + y.X; | |
result.Y = x.Y + y.Y; | |
result.Z = x.Z + y.Z; | |
} | |
public static void Add2(in Vec3 x, in Vec3 y, out Vec3 result) | |
{ | |
result.X = x.X + y.X; | |
result.Y = x.Y + y.Y; | |
result.Z = x.Z + y.Z; | |
} | |
public ref Vec3 Add3(Vec3 y, out Vec3 result) | |
{ | |
result.X = X + y.X; | |
result.Y = Y + y.Y; | |
result.Z = Z + y.Z; | |
return ref result; | |
} | |
public ref Vec3 Add4(in Vec3 y, out Vec3 result) | |
{ | |
result.X = X + y.X; | |
result.Y = Y + y.Y; | |
result.Z = Z + y.Z; | |
return ref result; | |
} | |
} | |
[CoreJob] | |
public class OpBenchmarks | |
{ | |
public Vec3 x = new Vec3(1000.0, 2000.0, 3000.0); | |
public Vec3 y = new Vec3(1000.0, 2000.0, 3000.0); | |
public Vec3 z = new Vec3(1000.0, 2000.0, 3000.0); | |
[Benchmark(Baseline = true)] | |
public Vec3 Operators() | |
{ | |
return x + y + z; | |
} | |
[Benchmark] | |
public Vec3 OutMethods() | |
{ | |
Vec3.Add1(x, y, out var res1); | |
Vec3.Add1(res1, z, out var res2); | |
return res2; | |
} | |
[Benchmark] | |
public Vec3 InOutMethods() | |
{ | |
Vec3.Add2(x, y, out var res1); | |
Vec3.Add2(res1, z, out var res2); | |
return res2; | |
} | |
[Benchmark] | |
public Vec3 RefReturnMethods() | |
{ | |
return x.Add3(y, out var _) | |
.Add3(z, out var _); | |
} | |
[Benchmark] | |
public Vec3 InRefReturnMethods() | |
{ | |
return x.Add4(y, out var _) | |
.Add4(z, out var _); | |
} | |
} | |
public static class Program | |
{ | |
static void Main() | |
{ | |
BenchmarkRunner.Run<OpBenchmarks>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment