Skip to content

Instantly share code, notes, and snippets.

@5argon
Created November 16, 2017 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5argon/8f99b1e9dfdf14f418623d64025a49ad to your computer and use it in GitHub Desktop.
Save 5argon/8f99b1e9dfdf14f418623d64025a49ad to your computer and use it in GitHub Desktop.
You can use ref and your own Lerp function to beat the performance of Vector3.LerpUnclamped (Vector3.Lerp is slower than Unclamped ones)
using UnityEngine;
using UnityEditor;
using NUnit.Framework;
using System.Collections.Generic;
using System.Diagnostics;
public class TestMisc {
delegate Vector3 CustomLerp(Vector3 v1, Vector3 v2, float lerp);
delegate void CustomLerpRef(ref Vector3 v1, Vector3 v2, float lerp);
delegate void CustomLerpRefRef(ref Vector3 v1, ref Vector3 v2, float lerp);
[Test]
public void LerpTestV1()
{
LerpTest(MyLerpV1);
}
[Test]
public void LerpTestV2()
{
LerpTest(MyLerpV2);
}
[Test]
public void LerpTestV3()
{
LerpTest(MyLerpV3);
}
[Test]
public void LerpTestV4()
{
LerpTest(MyLerpV4);
}
[Test]
public void LerpTestV5()
{
LerpTest(MyLerpV5);
}
[Test]
public void LerpTestUltimate()
{
LerpTest(MyLerpUltimate);
}
private void LerpTest(CustomLerp lerpFunction)
{
Stopwatch swUnityLerp = new Stopwatch();
Stopwatch swUnityLerpUnclamped = new Stopwatch();
Stopwatch swMyLerp = new Stopwatch();
for(int i = 0; i < 50000 ; i++)
{
Vector3 random1 = Random.insideUnitSphere;
Vector3 random2 = Random.insideUnitSphere;
Vector3 myLerp = random1;
Vector3 unityLerp = random1;
Vector3 unityLerpUnclamped = random1;
float randomLerp = Random.value;
swUnityLerp.Start();
unityLerp = Vector3.Lerp(random1, random2, randomLerp);
swUnityLerp.Stop();
swUnityLerpUnclamped.Start();
unityLerpUnclamped = Vector3.LerpUnclamped(random1, random2, randomLerp);
swUnityLerpUnclamped.Stop();
swMyLerp.Start();
myLerp = lerpFunction(random1, random2, randomLerp);
swMyLerp.Stop();
Assert.That(unityLerp.x, Is.EqualTo(myLerp.x).Within(0.000001));
Assert.That(unityLerp.y, Is.EqualTo(myLerp.y).Within(0.000001));
Assert.That(unityLerp.z, Is.EqualTo(myLerp.z).Within(0.000001));
}
long unityLerpTime = swUnityLerp.ElapsedTicks;
long unityLerpUnclampedTime = swUnityLerpUnclamped.ElapsedTicks;
long myLerpTime = swMyLerp.ElapsedTicks;
UnityEngine.Debug.LogFormat("My Lerp {0} vs. Unity Lerp {1} {2} ({3})", myLerpTime, unityLerpTime , unityLerpUnclampedTime , (unityLerpTime > myLerpTime && unityLerpUnclampedTime > myLerpTime) ? "I am faster!!" : "Unity is faster");
Assert.That(unityLerpTime, Is.GreaterThan(myLerpTime));
}
private void LerpTest(CustomLerpRef lerpFunction)
{
Stopwatch swUnityLerp = new Stopwatch();
Stopwatch swUnityLerpUnclamped = new Stopwatch();
Stopwatch swMyLerp = new Stopwatch();
for(int i = 0; i < 50000 ; i++)
{
Vector3 random1 = Random.insideUnitSphere;
Vector3 random2 = Random.insideUnitSphere;
Vector3 myLerp = random1;
Vector3 unityLerp = random1;
Vector3 unityLerpUnclamped = random1;
float randomLerp = Random.value;
swUnityLerp.Start();
unityLerp = Vector3.Lerp(random1, random2, randomLerp);
swUnityLerp.Stop();
swUnityLerpUnclamped.Start();
unityLerpUnclamped = Vector3.LerpUnclamped(random1, random2, randomLerp);
swUnityLerpUnclamped.Stop();
swMyLerp.Start();
lerpFunction(ref myLerp, random2, randomLerp);
swMyLerp.Stop();
Assert.That(unityLerp.x, Is.EqualTo(myLerp.x).Within(0.000001));
Assert.That(unityLerp.y, Is.EqualTo(myLerp.y).Within(0.000001));
Assert.That(unityLerp.z, Is.EqualTo(myLerp.z).Within(0.000001));
}
long unityLerpTime = swUnityLerp.ElapsedTicks;
long unityLerpUnclampedTime = swUnityLerpUnclamped.ElapsedTicks;
long myLerpTime = swMyLerp.ElapsedTicks;
UnityEngine.Debug.LogFormat("My Lerp {0} vs. Unity Lerp {1} {2} ({3})", myLerpTime, unityLerpTime , unityLerpUnclampedTime , (unityLerpTime > myLerpTime && unityLerpUnclampedTime > myLerpTime) ? "I am faster!!" : "Unity is faster");
Assert.That(unityLerpTime, Is.GreaterThan(myLerpTime));
}
private void LerpTest(CustomLerpRefRef lerpFunction)
{
Stopwatch swUnityLerp = new Stopwatch();
Stopwatch swUnityLerpUnclamped = new Stopwatch();
Stopwatch swMyLerp = new Stopwatch();
for(int i = 0; i < 50000 ; i++)
{
Vector3 random1 = Random.insideUnitSphere;
Vector3 random2 = Random.insideUnitSphere;
Vector3 myLerp = random1;
Vector3 unityLerp = random1;
Vector3 unityLerpUnclamped = random1;
float randomLerp = Random.value;
swUnityLerp.Start();
unityLerp = Vector3.Lerp(random1, random2, randomLerp);
swUnityLerp.Stop();
swUnityLerpUnclamped.Start();
unityLerpUnclamped = Vector3.LerpUnclamped(random1, random2, randomLerp);
swUnityLerpUnclamped.Stop();
swMyLerp.Start();
lerpFunction(ref myLerp, ref random2, randomLerp);
swMyLerp.Stop();
Assert.That(unityLerp.x, Is.EqualTo(myLerp.x).Within(0.000001));
Assert.That(unityLerp.y, Is.EqualTo(myLerp.y).Within(0.000001));
Assert.That(unityLerp.z, Is.EqualTo(myLerp.z).Within(0.000001));
}
long unityLerpTime = swUnityLerp.ElapsedTicks;
long unityLerpUnclampedTime = swUnityLerpUnclamped.ElapsedTicks;
long myLerpTime = swMyLerp.ElapsedTicks;
UnityEngine.Debug.LogFormat("My Lerp {0} vs. Unity Lerp {1} {2} ({3})", myLerpTime, unityLerpTime , unityLerpUnclampedTime , (unityLerpTime > myLerpTime && unityLerpUnclampedTime > myLerpTime) ? "I am faster!!" : "Unity is faster");
Assert.That(unityLerpTime, Is.GreaterThan(myLerpTime));
}
private Vector3 MyLerpV1(Vector3 v1, Vector3 v2, float lerp)
{
return v1 + (lerp * (v2-v1));
}
private Vector3 MyLerpV2(Vector3 v1, Vector3 v2, float lerp)
{
return new Vector3(
v1.x + (lerp * (v2.x - v1.x)),
v1.y + (lerp * (v2.y - v1.y)),
v1.z + (lerp * (v2.z - v1.z))
);
}
private Vector3 MyLerpV3(Vector3 v1, Vector3 v2, float lerp)
{
return new Vector3(
(lerp * v2.x) - ((lerp - 1) * v1.x),
(lerp * v2.y) - ((lerp - 1) * v1.y),
(lerp * v2.z) - ((lerp - 1) * v1.z)
);
}
private void MyLerpV4(ref Vector3 v1, Vector3 v2, float lerp)
{
v1.x += (lerp * (v2.x - v1.x));
v1.y += (lerp * (v2.y - v1.y));
v1.z += (lerp * (v2.z - v1.z));
}
private void MyLerpV5(ref Vector3 v1, ref Vector3 v2, float lerp)
{
v1.x += (lerp * (v2.x - v1.x));
v1.y += (lerp * (v2.y - v1.y));
v1.z += (lerp * (v2.z - v1.z));
}
private void MyLerpUltimate(ref Vector3 v1, ref Vector3 v2, float lerp)
{
v1.x += (lerp * v2.x) - (lerp * v1.x);
v1.y += (lerp * v2.y) - (lerp * v1.y);
v1.z += (lerp * v2.z) - (lerp * v1.z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment