Search Unity

What way is more efficient?

Discussion in 'Scripting' started by smirlianos, Aug 29, 2014.

?

What's better?

Poll closed Sep 5, 2014.
  1. Option 1#

    0 vote(s)
    0.0%
  2. Option 2#

    0 vote(s)
    0.0%
  1. smirlianos

    smirlianos

    Joined:
    Aug 2, 2012
    Posts:
    177
    I guess it doesn't affect the performance in any vizible level, but I will ask any way. :D

    I have this part of the script. (Keep in mind that it will be on many game objects at the same time)
    Only the Update part matters:

    On the first version, the system checks 2 statements every frame, but if I click the mouse button, nothing happens.

    Code (JavaScript):
    1. #pragma strict
    2. var selected : boolean;
    3.  
    4. function OnMouseUp () {
    5.     selected = true;
    6. }
    7.  
    8. function Update () {
    9.     if(Input.GetMouseButtonDown(0) && selected)
    10.     {
    11.         selected = false;
    12.     }
    13. }
    On the second version, the system checks only 1 statement evry frame, but a command is run every time I click the mouse button. (Which happens a lot)

    Code (JavaScript):
    1. #pragma strict
    2. var selected : boolean;
    3.  
    4. function OnMouseUp () {
    5.     selected = true;
    6. }
    7.  
    8. function Update () {
    9.     if(Input.GetMouseButtonDown(0))
    10.     {
    11.         selected = false;
    12.     }
    13. }
    So my question is, which version is lighter for the computer? Even theoriticaly...

    Thanks!
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If you only need to check input when selected is true then check selected first. If it's false then Input.GetMouseButtonDown(0) isn't even called even though it's in the same conditional.

    You'd also probably get better performance by checking that in one place and registering all selected GameObjects in a list.
     
  3. smirlianos

    smirlianos

    Joined:
    Aug 2, 2012
    Posts:
    177
    So you are telling me that inside an if statement, if the first check is false, the system does not check the other one at all?

    for example:

    Code (CSharp):
    1. if(A && B) {
    2.     //commands
    3. }
    if A = false, B is never checked
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    When using &&, yes that is correct.
     
  5. smirlianos

    smirlianos

    Joined:
    Aug 2, 2012
    Posts:
    177
    Thanks, I didn't know that!
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That being said - if you can check it in one place instead of a bunch of places then that's certainly the way to go.
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Not only when using &&. It does what it needs to satisfy the condition. It's called short circuiting. In this example:

    Code (csharp):
    1.  
    2. if(A || B)
    3. {
    4.   //do something
    5. }
    6.  
    If "A" is true it will never check B because A already satisfies the condition.
     
  8. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I believe KelsoMRK was referencing that if you used & it would check both.