However, it is difficult to test. You can do a few speedtests which give an intuition
Most of the time the switch-statement is faster:
TEST
Test for if-statement vs switch statement
package Switch;
public class SwitchSpeedTest {
void switchStatement(int loop) {
int temp = 0;
for (int i = 0; i < loop; i++) {
int value = 9;
switch (value) {
case 0:
temp = 9;
break;
case 1:
temp = 8;
break;
case 2:
temp = 7;
break;
case 3:
temp = 6;
break;
case 4:
temp = 5;
break;
case 5:
temp = 4;
break;
case 6:
temp = 3;
break;
case 7:
temp = 2;
break;
case 8:
temp = 1;
break;
case 9:
temp = 0;
break;
}
}
}
void ifStatement(int loop) {
int randomVariable = 0;
for (int i = 0; i < loop; i++) {
int value = 9;
if (value == 0)
randomVariable = 9;
else if (value == 1)
randomVariable = 8;
else if (value == 2)
randomVariable = 7;
else if (value == 3)
randomVariable = 6;
else if (value == 4)
randomVariable = 5;
else if (value == 5)
randomVariable = 4;
else if (value == 6)
randomVariable = 3;
else if (value == 7)
randomVariable = 2;
else if (value == 8)
randomVariable = 1;
else if (value == 9)
randomVariable = 0;
}
}
void start() {
long time;
long t;
int numberOfLoops = 1000;
System.out.println("warming up...");
switchStatement(numberOfLoops / 100);
ifStatement(numberOfLoops / 100);
// switch statement test
System.out.println("switch:");
t = 0;
for (int i = 0; i < numberOfLoops; i++) {
time = System.nanoTime();
switchStatement(numberOfLoops);
t += System.nanoTime() - time;
}
System.out.println(t / numberOfLoops);
// if statement test
System.out.println("if/else:");
t = 0;
for (int i = 0; i < numberOfLoops; i++) {
time = System.nanoTime();
ifStatement(numberOfLoops);
t += System.nanoTime() - time;
}
System.out.println(t / numberOfLoops);
}
public static void main(String[] args) {
new SwitchSpeedTest().start();
}
}
Test for nested if-statement vs switch statement
package Switch;
public class SvsNestedSpeedTest {
void switchStatement(int loop) {
int temp = 0;
for (int i = 0; i < loop; i++) {
int value = 9;
switch (value) {
case 0:
temp = 9;
break;
case 1:
temp = 8;
break;
case 2:
temp = 7;
break;
case 3:
temp = 6;
break;
case 4:
temp = 5;
break;
case 5:
temp = 4;
break;
case 6:
temp = 3;
break;
case 7:
temp = 2;
break;
case 8:
temp = 1;
break;
case 9:
temp = 0;
break;
}
}
}
void ifStatement(int loop) {
int randomVariable = 0;
for (int i = 0; i < loop; i++) {
int value = 9;
if (value == 0) {
randomVariable = 9;
} else {
if (value == 1) {
randomVariable = 8;
} else {
if (value == 2) {
randomVariable = 7;
} else {
if (value == 3) {
randomVariable = 6;
} else {
if (value == 4) {
randomVariable = 5;
} else {
if (value == 5) {
randomVariable = 4;
} else {
if (value == 6) {
randomVariable = 3;
} else {
if (value == 7) {
randomVariable = 2;
} else {
if (value == 8) {
randomVariable = 1;
} else {
if (value == 9) {
randomVariable = 0;
}
}
}
}
}
}
}
}
}
}
}
}
void start() {
long time;
long t;
int numberOfLoops = 1000;
System.out.println("warming up...");
switchStatement(numberOfLoops / 100);
ifStatement(numberOfLoops / 100);
// switch statement test
System.out.println("switch:");
t = 0;
for (int i = 0; i < numberOfLoops; i++) {
time = System.nanoTime();
switchStatement(numberOfLoops);
t += System.nanoTime() - time;
}
System.out.println(t / numberOfLoops);
// if statement test
System.out.println("if/else:");
t = 0;
for (int i = 0; i < numberOfLoops; i++) {
time = System.nanoTime();
ifStatement(numberOfLoops);
t += System.nanoTime() - time;
}
System.out.println(t / numberOfLoops);
}
public static void main(String[] args) {
new SvsNestedSpeedTest().start();
}
}
Conclusion
There is no difference between if-statements and nested if-statements. For a small example.