- Home /
Input - is it OK to use SWITCH?
is it ok to use SWITCH-CASE like this or should I use rather if-else? And here goes the most important question: which is more resource efficient? Meaning which uses up less memory? My script works withou the Input.anyKeyDown condition, but it doesn't work with it...why is that?
void KeyboardControl()
{
if(Input.GetKeyDown (KeyCode.R))
{
for(int i = 0; i<playerClonesHolder.transform.childCount; i++)
{
playerClonesHolder.transform.GetChild(i).FindChild ("dmgCube").GetComponent<DmgCube_scr>().ChargeStaff();
}
}
if(Input.anyKeyDown)
{
switch(Input.inputString)
{
case "1":
Debug.Log ("1 pressed");
break;
case "2":
Debug.Log ("2 pressed");
break;
case "3":
Debug.Log ("3 pressed");
break;
case "4":
Debug.Log ("4 pressed");
break;
case "5":
Debug.Log ("5 pressed");
break;
case "6":
Debug.Log ("6 pressed");
break;
case "7":
Debug.Log ("7 pressed");
break;
case "8":
Debug.Log ("8 pressed");
break;
case "9":
Debug.Log ("9 pressed");
break;
case "0":
Debug.Log ("0 pressed");
break;
default:
Debug.Log ("this is not a valid key");
break;
}
}
}
I'm impressed that you've gotten to the point in your project where you're optimizing down to the difference between a switch and an if. I'm also impressed that you've managed to pin point your biggest potential performance gain to your switch statement.
Alrighty, a less snarky response, then.
Really what it comes down to is personal preference. I, for example, tend towards switch statements in cases (pun intended) like you've illustrated above. I find it to be easier to read and maintain than a giant if/else if/else chain.
It also comes down to the specific problem you are trying to solve. You may find that there is no way to make a switch/case block to suit your needs, and thus are stuck with the if/else.
But again, all things equal, I tend towards the switch.
As far as performance of the two, I have no actual idea which is faster or why, nor do I find that I care. If you are seeing performance problems, the chances that they are tied to your choice of if or switch are so low it isn't even worth considering.
Thank you for the constructive comment. It may seem I keep asking dumb questions, but if that's the "case", it is because I'm a beginner and I want to make sure I'm doing right everything there is to be done. So if you are absolutely sure there's only negligible difference in resource efficiency, make your comment an answer and I'll accept it. Of course I would need you to answer also the next part of my question.
Answer by PaxForce · Oct 24, 2014 at 06:29 AM
I've got it:
for(int i = 0; i<10; i++)
{
if(Input.inputString == i.ToString ())
{
Debug.Log (i + " pressed");
}
}
Answer by Itaros · Oct 24, 2014 at 06:15 AM
Switch uses numerous optimization techniques like jump padding and selectiion by hashing. If you are using numbers(or enums or explicit numeric enums) for many possible outcomes it is much faster due to how proccessor conveyor works. If you switch by object hashing takes place and this becomes consideration factor requiring benchmarking. Regarding code in the ecxample: switch is the best way by performance and maintainability.
Additionali, I recommend to switch to unity keys enum for max performance gain
Your answer
![](https://koobas.hobune.stream/wayback/20220613165828im_/https://answers.unity.com/themes/thub/images/avi.jpg)