Last active
February 13, 2016 16:32
-
-
Save gok11/1bb347f11b5f9a3f77d6 to your computer and use it in GitHub Desktop.
GetComponent()の速度を改めて計測してみる ref: http://qiita.com/backlight1144/items/d2096c1369bb890ebbff
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 UnityEngine; | |
| public class DummyComponent : MonoBehaviour { } |
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 UnityEngine; | |
| public class GetComponentTest : MonoBehaviour { | |
| void Update () { | |
| GetComponent<TargetComponent>(); | |
| } | |
| } |
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 UnityEngine; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System; | |
| public class StopWatch : MonoBehaviour { | |
| private int tick = 0; | |
| private List<int> tickList = new List<int>(); | |
| void Update () { | |
| tick = Environment.TickCount; | |
| } | |
| void LateUpdate() | |
| { | |
| var delta = Environment.TickCount - tick; | |
| tickList.Add(delta); | |
| // サンプリングを10000回.その後ログ出力. | |
| if(tickList.Count == 9999) | |
| { | |
| Debug.Log("----- Average -----"); | |
| Debug.Log(tickList.Average().ToString("###.00")); | |
| Debug.Break(); | |
| } | |
| } | |
| } |
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 UnityEngine; | |
| public class TargetComponent : MonoBehaviour { } |
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 UnityEngine; | |
| public class TestManager : MonoBehaviour { | |
| [SerializeField] | |
| private GameObject pref; | |
| [SerializeField, ReadOnly] | |
| private int testObjCount = 1000; // オブジェクト数. | |
| private const int compCount = 1000; // Addするコンポーネント数. | |
| [SerializeField] | |
| private bool addTargetComponent = true; // GetComponent対象をAddするか | |
| [SerializeField, Range(0, compCount)] | |
| private int addIndex; // 何番目に対象コンポーネントをAddするか. | |
| void Awake() | |
| { | |
| for (int i = 0; i < testObjCount; i++) | |
| { | |
| var go = Instantiate(pref) as GameObject; | |
| go.AddComponent<GetComponentTest>(); | |
| for (int j = 0; j < compCount; j++) | |
| { | |
| AddTestComponent(go, j); | |
| } | |
| } | |
| } | |
| void AddTestComponent(GameObject go, int index) | |
| { | |
| if (addTargetComponent && | |
| index == addIndex) | |
| { | |
| go.AddComponent<TargetComponent>(); | |
| } | |
| else | |
| { | |
| go.AddComponent<DummyComponent>(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment