「毎日Unity」の技術ブログ

開発で役立つ情報を発信する

【UnityC#】==、GameObject.CompareTagの速度比較

==、GameObject.CompareTagの速度比較をしたので結果を残しておきます。

[ 環境 ]

Unity 2018.4.14.f1

[ 比較結果 ]

== 340 ms
GameObject.CompareTag 99 ms

[ スクリプト ]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PerformanceComparison : MonoBehaviour
{
    void Start()
    {
        System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch();

        int Count = 1000000;

        string Tag = "ABCDEFG";

        GameObject GameObject = new GameObject();
        GameObject.tag = Tag;

        //==

        StopWatch.Restart();

        for(int i = 0; i < Count; i++)
        {
            bool Temporary = GameObject.tag == Tag;
        }

        Debug.Log(StopWatch.ElapsedMilliseconds);

        //GameObject.CompareTag

        StopWatch.Restart();

        for(int i = 0; i < Count; i++)
        {
            bool Temporary = GameObject.CompareTag(Tag);
        }

        Debug.Log(StopWatch.ElapsedMilliseconds);
    }
}

[ 関連記事 ]