Search Unity

GameObject.Find performing better than GameObject.FindGameObjectWithTag

Discussion in 'Scripting' started by techmage, Dec 14, 2015.

  1. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    So I did some tests, here is the code:

    Code (CSharp):
    1.         using (new Timer("FindGameObjectWithTag"))
    2.         {
    3.             for (int i = 0; i < 10000; ++i)  
    4.             {
    5.                 var eventSystem = GameObject.FindGameObjectWithTag("EventSystem").GetComponent<EventSystem>();
    6.             }
    7.         }
    8.  
    9.         using (new Timer("FindObjectOfType"))
    10.         {
    11.             for (int i = 0; i < 10000; ++i)
    12.             {
    13.                 var eventSystem = FindObjectOfType<EventSystem>();
    14.             }
    15.         }
    16.  
    17.         using (new Timer("Find"))
    18.         {
    19.             for (int i = 0; i < 10000; ++i)
    20.             {
    21.                 var eventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
    22.             }
    23.         }

    Here are the results:

    Profiled FindGameObjectWithTag: 4.00ms
    Profiled FindObjectOfType: 33613.00ms
    Profiled Find: 3.00ms

    This is somewhat unexpected. I expected that FindGameObjectWithTag would be way faster than straight Find. As I assume FindGameObjectWithTag would only traverse the tags and what GameObject is associated to them. In the case of this project, like 6 tags total, with maybe 8 GameObjects attached to them, so I assumed it only needed to look through 8 GameObjects to find the one it needed.

    Whereas this scene has like 100 GameObjects in it, I assumed GameObject.Find had to test all of those until it found it. But it seems that isn't the case. I instantiated 100000 objects whose name started with 'A' and another 100000 objects whose name started with 'Z' thinking more objects in the scene would add time to the GameObject.Find search. But it made no difference, GameObject.Find takes exactly the same time with or without another 200000 objects added that start with 'A' or 'Z'.

    So I am a little confused here. How is GameObject.Find implemented? And why is it faster than FindGameObjectWithTag? It seems that FindGameObjectWithTag can be implemented so it most certainly is way faster than GameObject.Find
     
  2. GoGoGadget

    GoGoGadget

    Joined:
    Sep 23, 2013
    Posts:
    864
    That's kind of worrying that the FindObjectOfType performs so poorly? I always assumed it would perform in the same ballpark, bad assumption I guess :|
     
  3. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    I am bumping this thread because I want some clarification from a Unity dev on how GameObject.Find works.

    How does GameObject.Find work exactly? Can I rely on it having a 4 ms run time no matter how many object are in the scene? Does it use some kind of special mechanism that makes the amount of GameObject generally irrelevant to it's search time?