我使用else-if语句检查布尔值,由于使用else-if,这会导致性能下降。
基于这个原因,我正在考虑将else-if语句转换为switch语句,以便我希望听到您的建议,以适当地进行转换。
这是我的else-if陈述
public class Config_Monday_0 { private Context mContext;
public Config_Monday_0(Context context){ this.mContext = context; }
public int monday_0(TextView mon_1, TextView mon_2, TextView mon_3, TextView mon_4, TextView mon_5, TextView mon_6, TextView mon_7, TextView mon_8, TextView mon_9, TextView mon_10, TextView mon_11, TextView mon_12, TextView mon_13, TextView mon_14, TextView mon_15, TextView mon_16, TextView mon_17, TextView mon_18, TextView mon_19, TextView mon_20, TextView mon_21, TextView mon_22, TextView mon_23, TextView mon_24){
Schedule_Boolean_Monday_0 schedule_boolean_monday0 = new Schedule_Boolean_Monday_0(mContext);
DatabaseTimetable databaseTimetable = new DatabaseTimetable(mContext);
int start_time;
int end_time;
//Database 0 AM is not used
if (!schedule_boolean_monday0.Monday_0_1() && !schedule_boolean_monday0.Monday_0_2() && !schedule_boolean_monday0.Monday_0_3() && !schedule_boolean_monday0.Monday_0_4() && !schedule_boolean_monday0.Monday_0_5() &&
!schedule_boolean_monday0.Monday_0_6() && !schedule_boolean_monday0.Monday_0_7()&& !schedule_boolean_monday0.Monday_0_8() && !schedule_boolean_monday0.Monday_0_9() && !schedule_boolean_monday0.Monday_0_10() &&
!schedule_boolean_monday0.Monday_0_11() && !schedule_boolean_monday0.Monday_0_12() && !schedule_boolean_monday0.Monday_0_13() && !schedule_boolean_monday0.Monday_0_14() && !schedule_boolean_monday0.Monday_0_15() &&
!schedule_boolean_monday0.Monday_0_16() && !schedule_boolean_monday0.Monday_0_17()&& !schedule_boolean_monday0.Monday_0_18() && !schedule_boolean_monday0.Monday_0_19() && !schedule_boolean_monday0.Monday_0_20() &&
!schedule_boolean_monday0.Monday_0_21() && !schedule_boolean_monday0.Monday_0_22() && !schedule_boolean_monday0.Monday_0_23() && !schedule_boolean_monday0.Monday_0_24()){
return 0;
}
//0 am - 1 hr.
else if (schedule_boolean_monday0.Monday_0_1() && !schedule_boolean_monday0.Monday_0_2() && !schedule_boolean_monday0.Monday_0_3() && !schedule_boolean_monday0.Monday_0_4() && !schedule_boolean_monday0.Monday_0_5() &&
!schedule_boolean_monday0.Monday_0_6() && !schedule_boolean_monday0.Monday_0_7()&& !schedule_boolean_monday0.Monday_0_8() && !schedule_boolean_monday0.Monday_0_9() && !schedule_boolean_monday0.Monday_0_10() &&
!schedule_boolean_monday0.Monday_0_11() && !schedule_boolean_monday0.Monday_0_12() && !schedule_boolean_monday0.Monday_0_13() && !schedule_boolean_monday0.Monday_0_14() && !schedule_boolean_monday0.Monday_0_15() &&
!schedule_boolean_monday0.Monday_0_16() && !schedule_boolean_monday0.Monday_0_17()&& !schedule_boolean_monday0.Monday_0_18() && !schedule_boolean_monday0.Monday_0_19() && !schedule_boolean_monday0.Monday_0_20() &&
!schedule_boolean_monday0.Monday_0_21() && !schedule_boolean_monday0.Monday_0_22() && !schedule_boolean_monday0.Monday_0_23() && !schedule_boolean_monday0.Monday_0_24()){
start_time = 0;
end_time = 1;
schedule_boolean_monday0.Monday_0_1();
mon_1.setText(databaseTimetable.Monday_Title(start_time, end_time));
mon_1.setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start_time, end_time)));
mon_1.setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start_time, end_time)));
return 1;
}
//0 am - 2 hr.
else if (!schedule_boolean_monday0.Monday_0_1() && schedule_boolean_monday0.Monday_0_2() && !schedule_boolean_monday0.Monday_0_3() && !schedule_boolean_monday0.Monday_0_4() && !schedule_boolean_monday0.Monday_0_5() &&
!schedule_boolean_monday0.Monday_0_6() && !schedule_boolean_monday0.Monday_0_7()&& !schedule_boolean_monday0.Monday_0_8() && !schedule_boolean_monday0.Monday_0_9() && !schedule_boolean_monday0.Monday_0_10() &&
!schedule_boolean_monday0.Monday_0_11() && !schedule_boolean_monday0.Monday_0_12() && !schedule_boolean_monday0.Monday_0_13() && !schedule_boolean_monday0.Monday_0_14() && !schedule_boolean_monday0.Monday_0_15() &&
!schedule_boolean_monday0.Monday_0_16() && !schedule_boolean_monday0.Monday_0_17()&& !schedule_boolean_monday0.Monday_0_18() && !schedule_boolean_monday0.Monday_0_19() && !schedule_boolean_monday0.Monday_0_20() &&
!schedule_boolean_monday0.Monday_0_21() && !schedule_boolean_monday0.Monday_0_22() && !schedule_boolean_monday0.Monday_0_23() && !schedule_boolean_monday0.Monday_0_24()){
start_time = 0;
end_time = 2;
schedule_boolean_monday0.Monday_0_2();
mon_1.setText(databaseTimetable.Monday_Title(start_time, end_time));
mon_1.setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start_time, end_time)));
mon_1.setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start_time, end_time)));
mon_2.setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start_time, end_time)));
return 2;
}
//0 am - 3 hr.
else if (!schedule_boolean_monday0.Monday_0_1() && !schedule_boolean_monday0.Monday_0_2() && schedule_boolean_monday0.Monday_0_3() && !schedule_boolean_monday0.Monday_0_4() && !schedule_boolean_monday0.Monday_0_5() &&
!schedule_boolean_monday0.Monday_0_6() && !schedule_boolean_monday0.Monday_0_7()&& !schedule_boolean_monday0.Monday_0_8() && !schedule_boolean_monday0.Monday_0_9() && !schedule_boolean_monday0.Monday_0_10() &&
!schedule_boolean_monday0.Monday_0_11() && !schedule_boolean_monday0.Monday_0_12() && !schedule_boolean_monday0.Monday_0_13() && !schedule_boolean_monday0.Monday_0_14() && !schedule_boolean_monday0.Monday_0_15() &&
!schedule_boolean_monday0.Monday_0_16() && !schedule_boolean_monday0.Monday_0_17()&& !schedule_boolean_monday0.Monday_0_18() && !schedule_boolean_monday0.Monday_0_19() && !schedule_boolean_monday0.Monday_0_20() &&
!schedule_boolean_monday0.Monday_0_21() && !schedule_boolean_monday0.Monday_0_22() && !schedule_boolean_monday0.Monday_0_23() && !schedule_boolean_monday0.Monday_0_24()){
start_time = 0;
end_time = 3;
schedule_boolean_monday0.Monday_0_3();
mon_1.setText(databaseTimetable.Monday_Title(start_time, end_time));
mon_1.setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start_time, end_time)));
mon_1.setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start_time, end_time)));
mon_2.setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start_time, end_time)));
mon_3.setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start_time, end_time)));
return 3;
}
return 99;
}
}
这是一个指示布尔变量的类
public class Schedule_Boolean_Monday_0 {
private Context mContext;
private DatabaseTimetable databaseTimetable;
public Schedule_Boolean_Monday_0(Context context){
this.mContext = context;
}
public Boolean Monday_0_1(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,1);
return !result.equals("");
}
public Boolean Monday_0_2(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,2);
return !result.equals("");
}
public Boolean Monday_0_3(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,3);
return !result.equals("");
}
public Boolean Monday_0_4(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,4);
return !result.equals("");
}
public Boolean Monday_0_5(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,5);
return !result.equals("");
}
public Boolean Monday_0_6(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,6);
return !result.equals("");
}
public Boolean Monday_0_7(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,7);
return !result.equals("");
}
public Boolean Monday_0_8(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,8);
return !result.equals("");
}
public Boolean Monday_0_9(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,9);
return !result.equals("");
}
public Boolean Monday_0_10(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,10);
return !result.equals("");
}
public Boolean Monday_0_11(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,11);
return !result.equals("");
}
public Boolean Monday_0_12(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,12);
return !result.equals("");
}
public Boolean Monday_0_13(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,13);
return !result.equals("");
}
public Boolean Monday_0_14(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,14);
return !result.equals("");
}
public Boolean Monday_0_15(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,15);
return !result.equals("");
}
public Boolean Monday_0_16(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,16);
return !result.equals("");
}
public Boolean Monday_0_17(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,17);
return !result.equals("");
}
public Boolean Monday_0_18(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,18);
return !result.equals("");
}
public Boolean Monday_0_19(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,19);
return !result.equals("");
}
public Boolean Monday_0_20(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,20);
return !result.equals("");
}
public Boolean Monday_0_21(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,21);
return !result.equals("");
}
public Boolean Monday_0_22(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,22);
return !result.equals("");
}
public Boolean Monday_0_23(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,23);
return !result.equals("");
}
public Boolean Monday_0_24(){
databaseTimetable = new DatabaseTimetable(mContext);
String result = databaseTimetable.Monday_Title(0,0);
return !result.equals("");
}
}
每个if / else-if参数中都有24个布尔变量。我认为转换为切换方法可能会为我的应用带来更好的性能,如果您有任何想法,我想听听您的建议。
问题来源:stackoverflow
您的问题不是if语句,if语句尽可能高效。您的问题是每次访问变量时都要访问数据库,这是您应该处理的问题。
如果您是我,我将读取所有布尔变量一次,然后使用这些值来实现您要实现的任何逻辑。要点是READ ONCE AND CACHE ALL YOU CAN。
答案来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。