Skip to content

Instantly share code, notes, and snippets.

@inertiave
Created September 22, 2019 06:09
Show Gist options
  • Save inertiave/4958afbc0e8dfe0368ed8ea75a649466 to your computer and use it in GitHub Desktop.
Save inertiave/4958afbc0e8dfe0368ed8ea75a649466 to your computer and use it in GitHub Desktop.
https://www.sysnet.pe.kr/2/0/12005 를 참고해 gameObject 와 transform 을 호출 할 때의 속도 차이 비교
using System;
using System.Diagnostics;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class BuildTest : MonoBehaviour {
public Slider slider;
public Text countText;
public Button start;
public Text result;
int caseCount = 3000000;
Transform cachedTransform;
GameObject cachedGameObject;
Action<int, string, Action<int, StringBuilder>, StringBuilder> action;
void Awake()
{
cachedTransform = transform;
cachedGameObject = gameObject;
slider.onValueChanged.AddListener(value =>
{
caseCount = (int) value;
countText.text = caseCount.ToString();
});
start.onClick.AddListener(Do);
action = (loopCount, title, work, sb) =>
{
Stopwatch st = new Stopwatch();
st.Start();
work(loopCount, sb);
st.Stop();
sb.AppendLine(title + " : " + st.ElapsedMilliseconds + "ms.");
};
StringBuilder temp = new StringBuilder(500);
action(1, "bake", WorkGameObject, temp);
action(1, "bake", WorkCachedGameObject, temp);
action(1, "bake", WorkTransform, temp);
action(1, "bake", WorkCachedTransform, temp);
}
void Do()
{
StringBuilder sb = new StringBuilder(500);
action(caseCount, "GameObject", WorkGameObject, sb);
action(caseCount, "Cached GameObject", WorkCachedGameObject, sb);
action(caseCount, "Transform", WorkTransform, sb);
action(caseCount, "Cached Transform", WorkCachedTransform, sb);
result.text = sb.ToString();
}
void WorkGameObject(int count, StringBuilder logger)
{
GameObject go;
for (int i = 0; i < count; i++) {
go = gameObject;
}
}
void WorkCachedGameObject(int count, StringBuilder logger)
{
GameObject go;
for (int i = 0; i < count; i++) {
go = cachedGameObject;
}
}
void WorkTransform(int count, StringBuilder logger)
{
Transform trm;
for (int i = 0; i < count; i++) {
trm = transform;
}
}
void WorkCachedTransform(int count, StringBuilder logger)
{
Transform trm;
for (int i = 0; i < count; i++) {
trm = cachedTransform;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment