Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Performance - Multiple .cs files or an index

Discussion in 'Scripting' started by Ziron999, May 25, 2019.

  1. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    Example 1:
    Multiple .cs scripts attached to each individual gameobject
    Code (CSharp):
    1. public class SomeClass : Blahblah
    2. {
    3.     private void OnMouseOver()
    4.     {
    5.         ShowPopup(specialparam1);
    6.     }
    7.  
    8.     private void OnMouseExit()
    9.     {
    10.         RemovePopup();
    11.     }
    12. }
    Example 2:
    1 .cs script with an index and you identify it via the public int for each gameobject
    Code (CSharp):
    1. public class SomeClass : Blahblah
    2. {
    3. public int index;
    4.     private void OnMouseOver()
    5.     {
    6. if (index == 1)
    7. {
    8.         ShowPopup(specialparam1);
    9. }
    10. if (index == 2)
    11. {
    12.         ShowPopup(specialparam2);
    13. }
    14.     }
    15.  
    16.     private void OnMouseExit()
    17.     {
    18.         RemovePopup();
    19.     }
    20. }
    Example 3:
    1 .cs script with an index and you identify it via the public int for each gameobject
    Code (CSharp):
    1. public class SomeClass : Blahblah
    2. {
    3. public int index;
    4.     private void OnMouseOver()
    5.     {
    6. switch(index)
    7. {
    8. case 0:
    9.         ShowPopup(specialparam1);
    10. case 1:
    11.         ShowPopup(specialparam2);
    12. }
    13.     }
    14.  
    15.     private void OnMouseExit()
    16.     {
    17.         RemovePopup();
    18.     }
    19. }
    ect ect
    and let's say this is for about 20 things.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Theoretically, there will be small difference because the more scripts you have the more invocations of OnMouseOver you have, and determining wheter or not to invoke each incurs a tiny overhead - arguably similar to your own overhead (the decision to execute has to be made somewhere). A bigger (still tiny) overhead my be contributed by each individual script's Update() method.

    In reality, however, you should not worry about these things, and micro-managing tiny performance issues can be offset by a single unfortunate call within update (e.g. a GetComponnt<>() call). If you run into performance issues, use the profiler to identify and solve the big rocks, while leaving pebble and sand to the compiler and optimizer. Twenty additional invocations is next to nothing compared to the required visibility recalcs that ShowPopUp/RemovePopUp will cascade to.
     
    Last edited: May 25, 2019
  3. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    ya getcomponent is horrible. i initialize all arrays, getcomponents, ect, ect that i can and object pool quite a bit. i'm just trying to get down to the details. I've heard if it's more than 5 switch/case can start to show a massive difference the more there is then a if/else if. These things do matter when you start dealing with lots of things on the screen like an RTS with 1000+ 3d models. how do you handle the updates for movement? lots of questions that are detailed matter in these situations and make exponential differences.

    so that being said, yes i DO need to know these details and they DO matter.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I agree that they matter, and that you should keep attention to these things during the design phase. However, this is quite loew-level stuff, akin to loop-unrolling. I'm currently building a VR asteroid belt scene, with more than 4000 individual asteroids. Here the question was: should I attach a self-rotating script to each asteroid, or should I build an asteroid-field controller that rotates all asteroids, saving 4000 update calls. So I tried both, The difference was significantly less than 1 ms - the main performance killer was a camera effect that accounted for near 50% of frame time -- as I found out using the Profiler. so I recommend that you only look into low-level optimizations after you took care of finishing the game, removing all bugs and watering the lawn.
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Nnot knowing the details of the discussion wrt 5 switch cases killing performance - that sounds a lot like what happens if your compiled code grows outside the cache window causing branch prediction to revert to worst-case reloads. That usually applies to hand-optimized core code. You'd be doing a LOT of other optimizations before you tackle that kind of performance tweaks - especially since Unity (to my knowledge) does not give us much control over code generation --linking and -optimization.
     
  6. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    i probably will just do index because it's easier but it takes quite a lot of time to test example 1 vs 2 & 3 because 2 & 3 is easy it's within the same script. 1 needs like 20 scripts. the thing is each button has it's "own" mouseover call this way so i don't see how having tons of those would be a bad thing unless it's events. Since i don't really know how unity does this in the background... If each onmouseover is a event then i think an index switch/case would be ideal.

    i would think the code would be managed properly during compilation and all become one event somehow anyway.