LoginSignup
4
2

More than 5 years have passed since last update.

InspectorとGetComponentはどれくらい速度が違うのか調べてみる

Posted at

試した物

test.cs
public class measure_public : MonoBehaviour {

    public Text test_string;

    public void OnMeasure(){
        //スタート時間
        float _startTime = Time.realtimeSinceStartup;

        for (int i =0; i < 1000000; i++ ){
            test_string.text = "集計" + i;

        }

        //終了時間
        float _endTime = Time.realtimeSinceStartup;

        Debug.Log(_endTime - _startTime);


    }
}

test2.cs
public class measure_getcomponent : MonoBehaviour {

    public GameObject test_string;

    public void OnMeasure(){
        //スタート時間
        float _startTime = Time.realtimeSinceStartup;

        for (int i =0; i < 1000000; i++ ){
            test_string.GetComponent<Text>().text = "集計" + i;

        }

        //終了時間
        float _endTime = Time.realtimeSinceStartup;

        Debug.Log(_endTime - _startTime);


    }
}

すごくシンプルに上記で計算してみた。
test.cs 平均1.5秒
test2.cs 平均1.6秒

う〜ん。そんな違わない。
GetComponentはソースコード読んでいかないと,このファイルでは何が起こるか分からない。
だから,全部Inspectorに表示しようぜって方が好きではある。
でも,そんな速さに差はでない。

test3.cs
public class measure_findchild : MonoBehaviour {

    public GameObject Parent;

    public void OnMeasure(){
        //スタート時間
        float _startTime = Time.realtimeSinceStartup;

        for (int i =0; i < 1000000; i++ ){
            GameObject test_string = Parent.transform.FindChild("Text").gameObject;
            test_string.GetComponent<Text>().text = "集計" + i;

        }

        //終了時間
        float _endTime = Time.realtimeSinceStartup;

        Debug.Log(_endTime - _startTime);


    }
}

FindChildは重いぞ!と聞いていたので試してみた。
これは平均2.0秒。重いけどそんなでもないなーって感想。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2