package optimization01;
public class IfSwitchOptimization {
public void ifResult(int repeat, VariableRuleGeneratable value) {
int selected = 0;
StringBuilder sb = new StringBuilder();
long currentTime = System.currentTimeMillis();
for (int i = 0; i < repeat; i++) {
selected = value.result();
if (selected == 0) {
sb.append("영");
} else if (selected == 1) {
sb.append("일");
} else if (selected == 2) {
sb.append("이");
// 중간 생략
} else if (selected == 9) {
sb.append("구");
} else {
sb.append("무");
}
}
long resultTime = System.currentTimeMillis() - currentTime;
System.out.println(sb.toString());
System.out.println("if 연산 결과 : " + resultTime + "ms");
} // end of ifResult
public void switchResult(int repeat, VariableRuleGeneratable value) {
int selected = 0;
StringBuilder sb = new StringBuilder();
long currentTime = System.currentTimeMillis();
for (int i = 0; i < repeat; i++) {
selected = value.result();
switch (selected) {
case 0:
sb.append("영");
break;
case 1:
sb.append("일");
break;
case 2:
sb.append("이");
break;
// 중간 생략
case 9:
sb.append("구");
break;
default:
sb.append("무");
}
}
long resultTime = System.currentTimeMillis() - currentTime;
System.out.println(sb.toString());
System.out.println("switch 연산 결과 : " + resultTime + "ms");
} // end of switchResult
public void bigIfResult(int repeat, VariableRuleGeneratable value) {
int selected = 0;
StringBuilder sb = new StringBuilder();
long currentTime = System.currentTimeMillis();
for (int i = 0; i < repeat; i++) {
selected = value.result();
if (selected == 0) {
sb.append("영");
} else if (selected == 1) {
sb.append("일");
} else if (selected == 2) {
sb.append("이");
// 중간 생략
} else if (selected == 99) {
sb.append("구");
} else {
sb.append("무");
}
}
long resultTime = System.currentTimeMillis() - currentTime;
System.out.println(sb.toString());
System.out.println("if 연산 결과 : " + resultTime + "ms");
} // end of bigIfResult
public void bigSwitchResult(int repeat, VariableRuleGeneratable value) {
int selected = 0;
StringBuilder sb = new StringBuilder();
long currentTime = System.currentTimeMillis();
for (int i = 0; i < repeat; i++) {
selected = value.result();
switch (selected) {
case 0:
sb.append("영");
break;
case 1:
sb.append("일");
break;
case 2:
sb.append("이");
break;
// 중간 생략
case 99:
sb.append("구");
break;
default:
sb.append("무");
}
}
long resultTime = System.currentTimeMillis() - currentTime;
System.out.println(sb.toString());
System.out.println("switch 연산 결과 : " + resultTime + "ms");
} // end of bigSwitchResult
}