Possible Duplicate:
What is the relative performance difference of if/else versus switch statement in Java?
Given the following two methods:
public static int useSwitch(int i) {
switch (i) {
case 0:
return 1;
default:
return 0;
}
}
public static int useIf(int i) {
if (i == 0)
return 1;
return 0;
}
testing shows that the switch executes marginally faster (1.4 nanoseconds per call on my machine) than the if version.
I had always believed that the benefit of a switch didn't kick in until at least a few ifs could be avoided,
Why is switch faster than a single if?