Skip to content

Instantly share code, notes, and snippets.

@samloeschen
Last active March 4, 2021 01:40
Show Gist options
  • Save samloeschen/f9f2824c0ed1ac792c02e45ee19d7ab0 to your computer and use it in GitHub Desktop.
Save samloeschen/f9f2824c0ed1ac792c02e45ee19d7ab0 to your computer and use it in GitHub Desktop.
Mathf vs Mathematics
using Unity.Mathematics;
using UnityEngine;
using System.Diagnostics;
public class MathematicsTests : MonoBehaviour {
const int NUM_TESTS = 1_000_000;
void Test() {
Matrix4x4 identityMatrix4x4 = Matrix4x4.identity;
float3x3 identityFloat3x3 = new float3x3(1, 0, 0,
0, 1, 0,
0, 0, 1);
float4x4 identityFloat4x4 = new float4x4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
float vector3Duration, float3x3Duration, float4x4Duration;
Vector3 myVector3 = Vector3.one;
float3 myFloat3 = new float3(1f);
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < NUM_TESTS; i++) {
myVector3 = identityMatrix4x4.MultiplyPoint(myVector3);
}
stopwatch.Stop();
vector3Duration = stopwatch.ElapsedTicks / 10_000f;
stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < NUM_TESTS; i++) {
myFloat3 = math.mul(identityFloat3x3, myFloat3);
}
stopwatch.Stop();
float3x3Duration = stopwatch.ElapsedTicks / 10_000f;
stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < NUM_TESTS; i++) {
myFloat3 = math.mul(identityFloat4x4, new float4(myFloat3, 1f)).xyz;
}
stopwatch.Stop();
float4x4Duration = stopwatch.ElapsedTicks / 10_000f;
UnityEngine.Debug.Log("Elapsed Vector3: " + vector3Duration + "ms\n" +
"Elapsed float3x3: " + float3x3Duration + "ms\n" +
"Elapsed float4x4: " + float4x4Duration + "ms\n");
}
void OnEnable() => Test();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment