- Home /
How Expensive is Find function?
having
var noiseControl :NoiseControl;
At the moment i feed this variable through the inspector. Yet, for mobile game, how expensive will it be to just find it on Awake? Is it alright to initiate around 20 'Find's at startup assuming i have around 150-200 objects in scene? or perhaps have each object it's unique tag and FindObjectsWithTag?
is it so heavy i should not consider it at all?
Answer by Dave-Carlile · Jun 15, 2015 at 06:48 PM
In general you don't want to run Find
in an Update
function, but also in general it's fine in Start
. The answer to this depends entirely on your needs and your project and probably isn't something that can be answered definitively here.
You need to test it on the devices you'll be running on, and only you can determine if the resulting performance falls within your criteria of "this performance is good enough".
If I had to guess, running Find
20 times in an Awake
function is probably going to run just find. But I wouldn't take my word for it.
Answer by fafase · Jun 15, 2015 at 06:49 PM
It all depends how many and when. 100 in awake, if all awakes are run at the loading of the scene, then you are fine, you could set a splash screen to entertain during the 1s it may take.
Now if it happens during the game 100 of awakes may create a slight lag. Best would be to think either to have the inspector slot, or have a controller holding the needed references for a type and then use it when creating new objects.
How long it will take cannot be considered, you may have 1000 object and Find gets the right one first, or you have 10 objects and it is the last one. This is why you should consider finding whoever you need at the loading of the scene.
Now if you have one call here and there, it won't affect much.
Answer by The Stick · Jun 15, 2015 at 06:52 PM
If you're expecting to only Find
once ever (aka during startup) or even once in a while (ie every few seconds at minimum) then the performance hit should be tolerable/negligible. The big performance hits come from Find
ing several dozen times a frame.
The same should be said of the GetComponent
class of methods. While they aren't that bad as they have a rather limited scope (save GetComponentInChildren
, that can get messy) too many of them can cause a performance hit as well.
When in doubt, test, test, test. Thanks to the new Unity 5, you should be able to use the Profiler whether or not you paid for Unity. If you're looking for the expense of core Unity calls, I'd suggest you turn on Deep Profiling
before starting up your test.
Your answer
Follow this Question
Related Questions
How to hit a specific object 2 Answers
Find All FindObjectOfType() 1 Answer
Find object from another scene 1 Answer
Cant Find.() Text component in Canvas 2 Answers