Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active January 4, 2022 18:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/76ab23f0e3368e81c4259f6fe3c12b42 to your computer and use it in GitHub Desktop.
Save guitarrapc/76ab23f0e3368e81c4259f6fe3c12b42 to your computer and use it in GitHub Desktop.
Unity Benchmark for FindXxxx Method. refer : http://baba-s.hatenablog.com/entry/2014/07/09/093240
using System;
using UnityEngine;
/// <summary>
/// Find 関数の処理速度を計測するスクリプト
/// </summary>
public sealed class FindTest : MonoBehaviour
{
public int LoopCount = 1000; // 1 回のテストで Find 関数を実行する回数
public int TestCount = 10; // テストの回数
public int NumGameObjects = 100; // 検索対象のオブジェクトの数
/// <summary>
/// ゲーム開始時に指定された数分のオブジェクトを生成します
/// </summary>
private void Awake()
{
var types = new PrimitiveType[]
{
PrimitiveType.Capsule,
PrimitiveType.Cube,
PrimitiveType.Cylinder,
PrimitiveType.Plane,
PrimitiveType.Quad,
PrimitiveType.Sphere,
};
for (int i = 0; i < NumGameObjects; i++)
{
GameObject.CreatePrimitive(types[UnityEngine.Random.Range(0, types.Length)]);
}
}
/// <summary>
/// Find, FindGameObjectWithTag, FindObjectOfType 関数を実行するボタンを表示します
/// </summary>
private void OnGUI()
{
DrawButton(0, "Find", () => GameObject.Find("Player"));
DrawButton(100, "FindGameObjectWithTag", () => GameObject.FindGameObjectWithTag("Player"));
DrawButton(200, "FindObjectOfType", () => GameObject.FindObjectOfType<BoxCollider>());
}
/// <summary>
/// 指定された関数を実行するボタンを表示します
/// </summary>
private void DrawButton(float buttonY, string buttonText, Action findAct)
{
if (GUI.Button(new Rect(0, buttonY, Screen.width, 100), buttonText))
{
var sum = 0f;
for (int i = 0; i < TestCount; i++)
{
var time = Time.realtimeSinceStartup;
for (int j = 0; j < LoopCount; j++)
{
findAct();
}
time = Time.realtimeSinceStartup - time;
sum += time;
}
var avg = sum / TestCount;
Debug.Log(buttonText + ":" + avg + "sec");
}
}
}
@guitarrapc
Copy link
Author

guitarrapc commented Oct 12, 2016

Box Environment

Components Value
OS Windows 10 Pro 64bit (build 14393.223)
CPU Core-i7 6700 @3.40GHz
Memory 32GB
GPU GeForce 960

@guitarrapc
Copy link
Author

guitarrapc commented Oct 12, 2016

100 Objects

Unity Version Method LoopCount (1000) LoopCount (10000) LoopCount (100000)
5.4.0f3 Find 0.003656673sec 0.03624649sec 0.3626404sec
5.4.0f3 FindGameObjectWithTag 0.0001605987sec 0.00154953sec 0.01507263sec
5.4.0f3 FindObjectOfType 0.0658863sec 0.6607682sec 6.645772sec

1000 Objects

Unity Version Method LoopCount (1000) LoopCount (10000) LoopCount (100000)
5.4.0f3 Find 0.05269825sec 0.5394229sec 5.437216sec
5.4.0f3 FindGameObjectWithTag 0.0001548767sec 0.001549721sec 0.01499329sec
5.4.0f3 FindObjectOfType 0.1582977sec 1.623504sec 16.05754sec

10000 Objects

Unity Version Method LoopCount (1000) LoopCount (10000)
5.4.0f3 Find 0.8089469sec 8.789804sec
5.4.0f3 FindGameObjectWithTag 0.0001621246sec 0.00146637sec
5.4.0f3 FindObjectOfType 2.337319sec 24.55924sec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment