private static long checkIfTime(String str) {
long l = System.nanoTime();
if (str.contains("Internal_Version")) {
System.out.print("\tstr,\t");
} else if (str.contains("SM_FM")) {
System.out.print("\tstr,\t");
} else if (str.contains("MTK_Enginner")) {
System.out.print("\tstr,\t");
} else if (str.contains("SaleStat")) {
System.out.print("\tstr,\t");
} else if (str.contains("SAR")) {
System.out.print("\tstr,\t");
} else if (str.contains("Factory_Agent")) {
System.out.print("\tstr,\t");
} else if (str.contains("Normal_Agent")) {
System.out.print("\tstr,\t");
} else if (str.contains("Monkey_Test")) {
System.out.print("\tstr,\t");
} else if (str.contains("Customer_FM")) {
System.out.print("\tstr,\t");
} else if (str.contains("GOOGLE_ID")) {
System.out.print("\tstr,\t");
} else if (str.contains("HW_Info")) {
System.out.print("\tstr,\t");
} else if (str.contains("Sales_Setting")) {
System.out.print("\tstr,\t");
} else if (str.contains("SW_Version")) {
System.out.print("\tstr,\t");
} else if (str.contains("Custom_Factory_Mode")) {
System.out.print("\tstr,\t");
} else if (str.contains("Reset_Factory")) {
System.out.print("\tstr,\t");
} else if (str.contains("Sw_Version")) {
System.out.print("\tstr,\t");
}
long l1 = System.nanoTime();
System.out.print("if..else方式,消耗时间为:" + (l1 - l));
return l1 - l;
}
private static long checkSwitchTime(String str) {
long l = System.nanoTime();
switch (str) {
case "Internal_Version":
System.out.print("\tstr,\t");
break;
case "SM_FM":
System.out.print("\tstr,\t");
break;
case "MTK_Enginner":
System.out.print("\tstr,\t");
break;
case "SaleStat":
System.out.print("\tstr,\t");
break;
case "SAR":
System.out.print("\tstr,\t");
break;
case "Factory_Agent":
System.out.print("\tstr,\t");
break;
case "Normal_Agent":
System.out.print("\tstr,\t");
break;
case "Monkey_Test":
System.out.print("\tstr,\t");
break;
case "Customer_FM":
System.out.print("\tstr,\t");
break;
case "GOOGLE_ID":
System.out.print("\tstr,\t");
break;
case "HW_Info":
System.out.print("\tstr,\t");
break;
case "Sales_Setting":
System.out.print("\tstr,\t");
break;
case "SW_Version":
System.out.print("\tstr,\t");
break;
case "Custom_Factory_Mode":
System.out.print("\tstr,\t");
break;
case "Reset_Factory":
System.out.print("\tstr,\t");
break;
case "Sw_Version":
System.out.print("\tstr,\t");
break;
default:
break;
}
long l1 = System.nanoTime();
System.out.print("switch方式,消耗时间为:" + (l1 - l));
return l1 - l;
}
//比较switch 与 if..else的性能,取了三个值,多次比较
public static void main(String[] args) {
long internal_version = checkIfTime("Internal_Version");
long internal_version1 = checkSwitchTime("Internal_Version");
System.out.println(",消耗时间是它的" + (double) (internal_version / internal_version1) + "倍");
System.out.println("\t========================================分割线========================================");
long sw_version = checkIfTime("Sw_Version");
long sw_version1 = checkSwitchTime("Sw_Version");
System.out.println(",消耗时间是它的" + (double) (sw_version / sw_version1) + "倍");
System.out.println("\t========================================分割线========================================");
long Factory_Agent = checkIfTime("Factory_Agent");
long Factory_Agent1 = checkSwitchTime("Factory_Agent");
System.out.println(",消耗时间是它的" + (double) (Factory_Agent / Factory_Agent1) + "倍");
}
结论:
第一次,
str, if..else方式,消耗时间为:152626 str, switch方式,消耗时间为:16224,消耗时间是它的9.0倍
========================================分割线========================================
str, if..else方式,消耗时间为:19829 str, switch方式,消耗时间为:8413,消耗时间是它的2.0倍
========================================分割线========================================
str, if..else方式,消耗时间为:7210 str, switch方式,消耗时间为:5108,消耗时间是它的1.0倍
第二次,
str, if..else方式,消耗时间为:112666 str, switch方式,消耗时间为:13220,消耗时间是它的8.0倍
========================================分割线========================================
str, if..else方式,消耗时间为:18027 str, switch方式,消耗时间为:8112,消耗时间是它的2.0倍
========================================分割线========================================
str, if..else方式,消耗时间为:7511 str, switch方式,消耗时间为:5107,消耗时间是它的1.0倍
综合来看,大多数情况下,switch 性能比if..else要快几倍,少则一倍,多则七,八倍
网友评论