Im Übrigen muss ich antred zustimmen.
Es ist auch nicht so, dass aus jeder Anwendung das letzte an Leistung rausgekitzelt werden muss - wenn dem so ist, dann sollte man kein Java nehmen...
Es gibt durchaus Fälle, bei denen Wartbarkeit wichtiger als Leistung ist.
Edit:
Und nochmal an die Optimierer:
"Because the case expressions in the following switch statement are not contiguous values, the
compiler will likely translate the code into a comparison chain instead of a jump table:"
Compiler werden auch optimiert vom Hersteller... ich würde echt mal Benchmarks machen, vermutlich wird kein Unterschied bei beiden Variaten in der JRE festgestellt...
Edit2:
Code:
public class Parser {
public static int parseIf(char c) {
if (c==' ') {
return -1;
} else if (c=='+' || c=='-' || c=='/' || c=='*') {
return 0;
} else if (c=='(') {
return 1;
} else if (c==')') {
return 2;
} else {
return 3;
}
}
public static int parseSwitch(char c) {
switch (c) {
case ' ':
return -1;
case '+':
case '-':
case '/':
case '*':
return 0;
case '(':
return 1;
case ')':
return 2;
default:
return 3;
}
}
public static void main(final String[] args) {
String s = "+-/()((()))/**/*/-/-*-//*//*-/-*/*-/-*/*/*-/-*/++/+/+/+/+/+/+/+//**/+//+/+/*/*32/*+/+/*/+/+//*+/*/+*";
final long loops = 5000000;
// um Seiteneffekte zu vermeiden...
for (long i = 0; i < loops; i++) {
for (int j = 0; j < s.length(); j++) {
parseSwitch(s.charAt(j));
parseIf(s.charAt(j));
}
}
// Messen
long startI = System.currentTimeMillis();
for (long i = 0; i < loops; i++) {
for (int j = 0; j < s.length(); j++) {
parseIf(s.charAt(j));
}
}
long endI = System.currentTimeMillis();
long startS = System.currentTimeMillis();
for (long i = 0; i < loops; i++) {
for (int j = 0; j < s.length(); j++) {
parseSwitch(s.charAt(j));
}
}
long endS = System.currentTimeMillis();
System.out.println("If: " + (endI - startI) + " Switch: " + (endS - startS));
}
}
spuckt bei mir aus:
If: 4346 Switch: 4125
d.h. das sind 4,3 vs 4,1 Sekunden, und das bei 500.000.000 aufrufen an meinem Rechner... wenn der Leistungsunterschied hier nicht mal von essentieller Bedeutung ist...