I'm trying to test if switch statements execute faster than conditionals. The only thing is this is timing quantity instead of actual faster performance. What could I do to test the performance instead of simple quantity race tests?
class Statements {
public static void main(String args[]) {
int x = 0, y = 0, x1 = 0, y1 = 0;
double startTime = System.currentTimeMillis();
do {
switch (x) {
case 1:
x--;
y++;
break;
default:
x++;
break;
}
} while (y < 1000000000); //5.718, 6.736, 6.124
double endTime = System.currentTimeMillis();
System.out.println((endTime - startTime) / 1000 + " seconds");
double startTime1 = System.currentTimeMillis();
do {
if (x1 < 1) {
y1++;
x1++;
} else {
x1--;
}
} while (y1 < 1000000000);
double endTime1 = System.currentTimeMillis();
System.out.println((endTime1 - startTime1) / 1000 + " seconds");
}
}