Update function (Performance question)...

i know the Update function is in the black list when we talk about performance things.

i have two scripts:

ScriptA.js

function Update () {

if (connect)     DoActionA();

}

ScriptB.js

function Update () {
if (connect)    DoActionB();

}

i have two script with their own Update functions…

Now, what if i concatenate all of that into just one script?

ScriptAB.js

function Update (){
if (connect){

     DoActionA();
     DoActionB();
     }
}

Does this will help me to get performance?.. or is just the same thing?? (Think it for Android or Mobile devices)
Thank you

I’m trying to get it through logic, and then it’s better to use ScriptAB.js, why:

  • Using more scripts will first off increase file size (even if it is a couple of bytes).
  • It is always better to use the same if-statement for more actions than doing them seperately.

So using ScriptAB.js is certainly better than using them seperately, you’re decreasing file size to make advantage of the low memory space on a mobile and you’re combining both statements into one, so that it will decrease processor usage (your processor will just have to go through one check instead of two).

Whether combining those scripts is a good idea depends on what DoActionA and DoActionB are supposed to do.

both scripts are using the same game objects…
i thinking in concatenate both scripts…

Thanks both…Q

Really? That’s new to me. Sure, you don’t put any crazy thing in there, but I don’t see why it should be on a black list. It’s like saying “eating makes you fat”… Well, duh, but it’s not like you can’t avoid eating, can’t you?

Well, yeah. You saved a method call (one Update call), which account for about one memory access (or stack call)… So you saved about a fraction of a nanosecond. I think someone did the math a while ago and found out you would have to do that 400,000 times to save one millisecond (ms).

In other words, if the two script have logical reason to be apart, leave them apart. Removing one method from a script for “optimization” purpose is just about the worst optimization you can imagine.

It’s quite a bit slower than that since running Update involves reflection. However it’s true that doing this for two objects is worthless. 100 objects would make more sense, but indeed the primary consideration should be logic and readability first. When you’re done, and if profiling indicates that Update is a bottleneck, then you can change it.

–Eric

Weird. I was sure Unity built a list of script implementing Update and would simply iterate on it as if it was an interface implementation. Why would they invoke reflection once it is known which script has Update or not?