[Curios Question] What is best update function vs loop ?

Updated on June 25, 2016 in Discussions
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on June 25, 2016

I was wondering what is more fast among Update Function and loop.
For e.g.
I have a big code in block { very big code } which will be executed on 100 Enemy each.

    • Case Update  function :
      Each  Enemy will have there own running update function and in each of enemy { very big code } will be executed .
    • Case Loop :
      Update() { // one unique Update
           for ( var i = 0 ; i < TotalEnemys ; i++)
          {
               Enemys[i] = { very big code } ;
           }
      }
       

Now , which one of case will be best ?

I Asked this question because ,
” In Unity when 100 gameobjects are instantiated each have there own update function which keeps updating , i previous also started using few other html5 gaming frameworks since i needed more performance over browser since unity webgl still lacks lots things . When i tried few frameworks i found that it only had one unique update functions for all game stuff”

I know its seriously idiotic question but still curious .

By the way do someone thinks that  there should be a “Just Curious” Topic ?

  • Liked by
Reply
1 on June 25, 2016

Probably the loop because update function is constantly running, whereas the loop you can break at anytime. I might be wrong here though.

Devoted
on June 25, 2016

Yea but breaking loop will cause some enemys to stop .

Show more replies
  • Liked by
Reply
Cancel
3 on June 25, 2016

I would say use a foreach loop if it’s for every enemy and a for loop if not. This is because an Update function is still highly fps dependent and could slow down your game a lot faster, I think, then a loop would.

Also for the just curious category I don’t think there should be one this is what the Discussions category is for 😉

on June 25, 2016

A for loop for all kinds of scenario? foreach loop seems to be slower as it calls a coroutine

Guru
on June 25, 2016

Depends on the size of the array you are trying to do with it but if you have hundreds of objects both Update and a for loop would slow it down. You just need to get to know when to use what to achieve the best performance.

Guru
on June 25, 2016

A foreach is never faster than a for loop because it is essentially a for loop with some extra stuff attached to it. Often it’s slower and sometimes just as fast as a for loop.

Show more replies
  • Liked by
Reply
Cancel
0 on June 25, 2016

Unity has the (managed) C# mono side of the code and the (unmanaged) C++ side. Update (managed) in a script gets called from the (unmanaged) C++ side. Because of that it needs to go through a huge checklist to actually call the method.
 
So many Updates costs performance.
I think the loop will be better.

  • Liked by
Reply
Cancel
0 on June 25, 2016

Thx for the all replies . I had two small test . First with 1000 updates / loop in update for getting the fps . Theres lot difference between updates and loop , were loop was less , which is good.

Finally i tested same with 100000 update functions vs 100000 lenght loop a single update . The result was quit amazing .
Here  i found that ,
Update functions were running on there on with respect to time , giving same fps (with only slight difference) without causing in other lag outbreaks .
whereas ,
Loop seemed to freeze my lap every time it gets updated causing lot of things like not responding or heating suddenly and so on .

Finally my thoughs : Try to minimize loops when the length is more than 10,000 if its performance . Never Exceed 40,000/50,000 loop length . It will start causing freeze depending upon device . Try breaking code when ever posible .Sometimes giving a chance to update might be good . But still it dosnt mean you should have 100,000 update functions 🙂

  • Liked by
Reply
Cancel