Last active
March 4, 2021 01:40
-
-
Save samloeschen/f9f2824c0ed1ac792c02e45ee19d7ab0 to your computer and use it in GitHub Desktop.
Mathf vs Mathematics
This file contains hidden or 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 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