Created
April 24, 2018 13:57
-
-
Save nitroxis/c700ef0f4bbe28cbc3090108410d4034 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.Runtime.CompilerServices; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
public struct Matrix4x4 | |
{ | |
public float M11, M12, M13, M14; | |
public float M21, M22, M23, M24; | |
public float M31, M32, M33, M34; | |
public float M41, M42, M43, M44; | |
public static void Add(in Matrix4x4 left, in Matrix4x4 right, out Matrix4x4 result) | |
{ | |
result.M11 = left.M11 + right.M11; result.M12 = left.M12 + right.M12; result.M13 = left.M13 + right.M13; result.M14 = left.M14 + right.M14; | |
result.M21 = left.M21 + right.M21; result.M22 = left.M22 + right.M22; result.M23 = left.M23 + right.M23; result.M24 = left.M24 + right.M24; | |
result.M31 = left.M31 + right.M31; result.M32 = left.M32 + right.M32; result.M33 = left.M33 + right.M33; result.M34 = left.M34 + right.M34; | |
result.M41 = left.M41 + right.M41; result.M42 = left.M42 + right.M42; result.M43 = left.M43 + right.M43; result.M44 = left.M44 + right.M44; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AddInline(in Matrix4x4 left, in Matrix4x4 right, out Matrix4x4 result) | |
{ | |
result.M11 = left.M11 + right.M11; result.M12 = left.M12 + right.M12; result.M13 = left.M13 + right.M13; result.M14 = left.M14 + right.M14; | |
result.M21 = left.M21 + right.M21; result.M22 = left.M22 + right.M22; result.M23 = left.M23 + right.M23; result.M24 = left.M24 + right.M24; | |
result.M31 = left.M31 + right.M31; result.M32 = left.M32 + right.M32; result.M33 = left.M33 + right.M33; result.M34 = left.M34 + right.M34; | |
result.M41 = left.M41 + right.M41; result.M42 = left.M42 + right.M42; result.M43 = left.M43 + right.M43; result.M44 = left.M44 + right.M44; | |
} | |
public static Matrix4x4 operator +(Matrix4x4 left, Matrix4x4 right) | |
{ | |
Matrix4x4 result; | |
result.M11 = left.M11 + right.M11; result.M12 = left.M12 + right.M12; result.M13 = left.M13 + right.M13; result.M14 = left.M14 + right.M14; | |
result.M21 = left.M21 + right.M21; result.M22 = left.M22 + right.M22; result.M23 = left.M23 + right.M23; result.M24 = left.M24 + right.M24; | |
result.M31 = left.M31 + right.M31; result.M32 = left.M32 + right.M32; result.M33 = left.M33 + right.M33; result.M34 = left.M34 + right.M34; | |
result.M41 = left.M41 + right.M41; result.M42 = left.M42 + right.M42; result.M43 = left.M43 + right.M43; result.M44 = left.M44 + right.M44; | |
return result; | |
} | |
public static Matrix4x4 operator -(in Matrix4x4 left, in Matrix4x4 right) | |
{ | |
Matrix4x4 result; | |
result.M11 = left.M11 + right.M11; result.M12 = left.M12 + right.M12; result.M13 = left.M13 + right.M13; result.M14 = left.M14 + right.M14; | |
result.M21 = left.M21 + right.M21; result.M22 = left.M22 + right.M22; result.M23 = left.M23 + right.M23; result.M24 = left.M24 + right.M24; | |
result.M31 = left.M31 + right.M31; result.M32 = left.M32 + right.M32; result.M33 = left.M33 + right.M33; result.M34 = left.M34 + right.M34; | |
result.M41 = left.M41 + right.M41; result.M42 = left.M42 + right.M42; result.M43 = left.M43 + right.M43; result.M44 = left.M44 + right.M44; | |
return result; | |
} | |
public static Matrix4x4 operator *(in Matrix4x4 left, in Matrix4x4 right) | |
{ | |
Matrix4x4.Add(in left, in right, out Matrix4x4 result); | |
return result; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Matrix4x4 operator /(in Matrix4x4 left, in Matrix4x4 right) | |
{ | |
Matrix4x4.Add(in left, in right, out Matrix4x4 result); | |
return result; | |
} | |
public static Matrix4x4 operator &(in Matrix4x4 left, in Matrix4x4 right) | |
{ | |
Matrix4x4.AddInline(in left, in right, out Matrix4x4 result); | |
return result; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Matrix4x4 operator |(in Matrix4x4 left, in Matrix4x4 right) | |
{ | |
Matrix4x4.AddInline(in left, in right, out Matrix4x4 result); | |
return result; | |
} | |
} | |
public class Matrix4x4Benchmark | |
{ | |
private static readonly Matrix4x4 testMatrix = new Matrix4x4 | |
{ | |
M11 = 0.11f, M12 = 0.12f, M13 = 0.13f, M14 = 0.14f, | |
M21 = 0.21f, M22 = 0.22f, M23 = 0.23f, M24 = 0.24f, | |
M31 = 0.31f, M32 = 0.32f, M33 = 0.33f, M34 = 0.34f, | |
M41 = 0.41f, M42 = 0.42f, M43 = 0.43f, M44 = 0.44f, | |
}; | |
[Benchmark] | |
public Matrix4x4 StaticMethod() | |
{ | |
Matrix4x4 a = testMatrix; | |
Matrix4x4 b = testMatrix; | |
Matrix4x4 c; | |
Matrix4x4.Add(in a, in b, out c); | |
Matrix4x4.Add(in c, in a, out b); | |
Matrix4x4.Add(in b, in c, out a); | |
Matrix4x4.Add(in a, in b, out c); | |
Matrix4x4.Add(in c, in a, out b); | |
Matrix4x4.Add(in b, in c, out a); | |
return a; | |
} | |
[Benchmark] | |
public Matrix4x4 StaticMethodInline() | |
{ | |
Matrix4x4 a = testMatrix; | |
Matrix4x4 b = testMatrix; | |
Matrix4x4 c; | |
Matrix4x4.AddInline(in a, in b, out c); | |
Matrix4x4.AddInline(in c, in a, out b); | |
Matrix4x4.AddInline(in b, in c, out a); | |
Matrix4x4.AddInline(in a, in b, out c); | |
Matrix4x4.AddInline(in c, in a, out b); | |
Matrix4x4.AddInline(in b, in c, out a); | |
return a; | |
} | |
[Benchmark] | |
public Matrix4x4 StandardOperator() | |
{ | |
Matrix4x4 a = testMatrix; | |
Matrix4x4 b = testMatrix; | |
Matrix4x4 c; | |
c = a + b; | |
b = c + a; | |
a = b + c; | |
c = a + b; | |
b = c + a; | |
a = b + c; | |
return a; | |
} | |
[Benchmark] | |
public Matrix4x4 InOperator() | |
{ | |
Matrix4x4 a = testMatrix; | |
Matrix4x4 b = testMatrix; | |
Matrix4x4 c; | |
c = a - b; | |
b = c - a; | |
a = b - c; | |
c = a - b; | |
b = c - a; | |
a = b - c; | |
return a; | |
} | |
[Benchmark] | |
public Matrix4x4 InOperatorCallsStaticMethod() | |
{ | |
Matrix4x4 a = testMatrix; | |
Matrix4x4 b = testMatrix; | |
Matrix4x4 c; | |
c = a * b; | |
b = c * a; | |
a = b * c; | |
c = a * b; | |
b = c * a; | |
a = b * c; | |
return a; | |
} | |
[Benchmark] | |
public Matrix4x4 InOperatorInlineCallsStaticMethod() | |
{ | |
Matrix4x4 a = testMatrix; | |
Matrix4x4 b = testMatrix; | |
Matrix4x4 c; | |
c = a / b; | |
b = c / a; | |
a = b / c; | |
c = a / b; | |
b = c / a; | |
a = b / c; | |
return a; | |
} | |
[Benchmark] | |
public Matrix4x4 InOperatorCallsStaticMethodInline() | |
{ | |
Matrix4x4 a = testMatrix; | |
Matrix4x4 b = testMatrix; | |
Matrix4x4 c; | |
c = a & b; | |
b = c & a; | |
a = b & c; | |
c = a & b; | |
b = c & a; | |
a = b & c; | |
return a; | |
} | |
[Benchmark] | |
public Matrix4x4 InOperatorInlineCallsStaticMethodInline() | |
{ | |
Matrix4x4 a = testMatrix; | |
Matrix4x4 b = testMatrix; | |
Matrix4x4 c; | |
c = a | b; | |
b = c | a; | |
a = b | c; | |
c = a | b; | |
b = c | a; | |
a = b | c; | |
return a; | |
} | |
} | |
static class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<Matrix4x4Benchmark>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment