Skip to content

Instantly share code, notes, and snippets.

@ThinhHB
Last active December 19, 2016 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThinhHB/b3d00c8d70313d9519dcfce7979bb256 to your computer and use it in GitHub Desktop.
Save ThinhHB/b3d00c8d70313d9519dcfce7979bb256 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
public class TestCacheComponentPerformance : MonoBehaviour
{
/// Will use for Loop test in Update
[SerializeField] int runCount = 100000;
/// Use for Editor testing, change the runCount, and press this bool
/// in Inspector to run the test again
[SerializeField] bool test = false;
void Update ()
{
if (test)
{
RunTest();
test = false;
}
}
void RunTest()
{
Stopwatch watch = new Stopwatch();
var cacheTransform = transform;
// test direct transform
watch.Start();
for (int m = 0; m < runCount; m++)
{
transform.position = Vector3.zero;
}
watch.Stop();
Debug.Log("Direct call : " + watch.ElapsedMilliseconds);
watch.Reset();
// test getcomponent
watch.Start();
for (int m = 0; m < runCount; m++)
{
GetComponent<Transform>().position = Vector3.zero;
}
watch.Stop();
Debug.Log("GetComponent call : " + watch.ElapsedMilliseconds);
watch.Reset();
// test cache
watch.Start();
for (int m = 0; m < runCount; m++)
{
cacheTransform.position = Vector3.zero;
}
watch.Stop();
Debug.Log("Cached call : " + watch.ElapsedMilliseconds);
watch.Reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment