# Update function (Performance question)...

**URL:** https://discussions.unity.com/t/update-function-performance-question/524454
**Category:** Performance & Profiling
**Tags:** Scripting
**Created:** [January 1, 2014, 3:46pm UTC](https://discussions.unity.com/t/update-function-performance-question/524454 "2014-01-01T15:46:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![LawyerOfGod](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lawyerofgod/32/477378_2.png) [@LawyerOfGod](https://discussions.unity.com/u/LawyerOfGod)
#### Post date: [January 1, 2014, 3:46pm UTC](https://discussions.unity.com/t/update-function-performance-question/524454/1 "2014-01-01T15:46:30Z")

</div>

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

i have two scripts:

ScriptA.js

```auto
function Update () {

if (connect) DoActionA();

}

```

ScriptB.js

```auto
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

```auto
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

---

<div class="post-metadata">

### Author: ![HungrySnake](https://avatars.discourse-cdn.com/v4/letter/h/7ba0ec/32.png) [@HungrySnake](https://discussions.unity.com/u/HungrySnake)
#### Post date: [January 1, 2014, 4:40pm UTC](https://discussions.unity.com/t/update-function-performance-question/524454/2 "2014-01-01T16:40:39Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![Zeblote](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/zeblote/32/304391_2.png) [@Zeblote](https://discussions.unity.com/u/Zeblote)
#### Post date: [January 1, 2014, 5:23pm UTC](https://discussions.unity.com/t/update-function-performance-question/524454/3 "2014-01-01T17:23:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![LawyerOfGod](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lawyerofgod/32/477378_2.png) [@LawyerOfGod](https://discussions.unity.com/u/LawyerOfGod)
#### Post date: [January 1, 2014, 6:15pm UTC](https://discussions.unity.com/t/update-function-performance-question/524454/4 "2014-01-01T18:15:03Z")

</div>

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

Thanks both…Q

---

<div class="post-metadata">

### Author: ![LightStriker1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lightstriker1/32/301826_2.png) [@LightStriker1](https://discussions.unity.com/u/LightStriker1)
#### Post date: [January 1, 2014, 9:16pm UTC](https://discussions.unity.com/t/update-function-performance-question/524454/5 "2014-01-01T21:16:32Z")

</div>

> [@LawyerOfGod](#):
>
> i know the Update function is in the black list when we talk about performance things.

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?

> [@LawyerOfGod](#):
>
> Does this will help me to get performance?.. or is just the same thing?? (Think it for Android or Mobile devices)  
> Thank 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.

---

<div class="post-metadata">

### Author: ![Eric5h5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eric5h5/32/44225_2.png) [@Eric5h5](https://discussions.unity.com/u/Eric5h5)
#### Post date: [January 1, 2014, 9:25pm UTC](https://discussions.unity.com/t/update-function-performance-question/524454/6 "2014-01-01T21:25:45Z")

</div>

> [@LightStriker\_1](#):
>
> Well, yeah. You saved a method call (one Update call), which account for about one memory access (or stack call)…

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

---

<div class="post-metadata">

### Author: ![LightStriker1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lightstriker1/32/301826_2.png) [@LightStriker1](https://discussions.unity.com/u/LightStriker1)
#### Post date: [January 1, 2014, 10:28pm UTC](https://discussions.unity.com/t/update-function-performance-question/524454/7 "2014-01-01T22:28:28Z")

</div>

> [@Eric5h5](#):
>
> 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?
