Last active
December 19, 2016 02:15
-
-
Save ThinhHB/b3d00c8d70313d9519dcfce7979bb256 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 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