If statement efficiency question

Is an if statement running every frame more efficient than running the same line of code every frame. For example:

void Update()
    {
        if (Input.GetKey(KeyCode.F))
        {
            if (Cursor.lockState == CursorLockMode.Locked) 
            {
                Cursor.lockState = CursorLockMode.None;
            }
        }

        else
        {
            if (Cursor.lockState == CursorLockMode.None) 
        }

VS

void Update()
    {
        if (Input.GetKey(KeyCode.F))
        {
            Cursor.lockState = CursorLockMode.None;
        }

        else
        {
            Cursor.lockState = CursorLockMode.Locked;
        }
    }

Which code example would run more efficient? (I don’t really think choosing one option over the other makes too much of a difference performance wise but I’m just interested in learning how this works).

If you look at the source of Cursor.lockState you will note that a call is made to the C++ side of the engine. So just doing a check to see in which state lockState is will cause that invoke rather than be a simple enum return value.

I can not see the engine’s C++ side but I’m pretty sure they would check if the lockState is not already in the requested state before setting whatever needs to be set. So doing your own conditional check in that case makes it already pointless.

So at end, you 2nd block of code is fine except if you really want to do this micro “optimization” in which case you need to track the state with your own variable to do the state check in C# side without the invoke.

I have also thought on this problem, with no real answer given. So just by default, the way I write code, is to always offer a check, and make sure it only runs once.

void Update()
     {
         if (Input.GetKey(KeyCode.F))
         {
             if (Cursor.lockState != CursorLockMode.None)
             { Cursor.lockState = CursorLockMode.None; }
         }
 
         else
         {
             if (Cursor.lockState != CursorLockMode.Locked)
             { Cursor.lockState = CursorLockMode.Locked; }
         }
     }

My thought on this, is while in an if statement is running, a check of sorts would always have to run. So hopefully internally these checks are set to a cached reference. Basically meaning cursorState == 752661, and its mode = 553374, so the compiler see’s it as such, and is no real computing power being used.

Now however when you go to change a variable, I’m sure the compiler switches between multiple scripts, gets the info it needs, and then overwrites what you want it to overwrite. So instead of a simple variable check, your now using processing power to lookAt > Get > Set > Verify > Return.

If that makes any sense? But as I say, this is the way I view the code, and how I stop it from doing any more processes than absolutely needed. :slight_smile:

EDIT: In regards to specifics, if you were worried about CusorMode a fellow dev(Infindustry dev), pointed out that internally, that call may be no different than the set. So setting it up this way would be a small performance boost(micro improvement):

private bool lockState = CursorLockMode.Locked;
protected void Start()
{
  lockState = CursorLockMode.Locked;
  Cursor.lockState = CursorLockMode.Locked;
}

protected void Update()
{
  if (Input.GetKey(KeyCode.F))
  {
    if (lockState != CursorLockMode.None) 
    {
      lockState = CursorLockMode.None;
      Cursor.lockState = CursorLockMode.None;
    }
  }
 else if (lockState != CursorLockMode.Locked)
  {
    lockState = CursorLockMode.Locked;
    Cursor.lockState = CursorLockMode.Locked;
  }
}

So all in all, it really depends on the context. If you must have a certain question do what it does, you need to find the way it’s written internally, and modify it the best you can to use the less ms, GC, or CPU usage it can.