所以我剛剛得到了一堆舊代碼,我應該在其中提高性能。找了一段時間后,我遇到了這段代碼:
//Reference on how the global Variables look like:
private static final int PLS_4_SYSTEM_MALFUNCTION_FL = 4;
private static final int SPS1_20_PUFFER_EMPTY = 20;
private static final int SPS1_30_STOPPERFL_LOCKED = 30;
//...
int disturbance1 = bMessage.getDisturbanceSps1().intValue();
int disturbance2 = bMessage.getDisturbanceSps1().intValue();
if (disturbance1 == SPS1_20_PUFFER_EMPTY && disturbance2 == 0) {
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
}
if (disturbance1 == SPS1_30_STOPPERFL_LOCKED && disturbance2 == 0) {
checkWarnings(bMessage, PLS_6_STOPPERFL_LOCKED);
}
if (disturbance1 == SPS1_40_DISTURBANCEFL && disturbance2 == 0) {
if (bMessage.getDisturbanceType().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_SHORT) {
checkWarnings(bMessage, PLS_3_SHORTDISTURBANCE_FL);
}
else if (bMeldung.getStoerungsartMde().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_LONG) {
checkWarnings(bMessage, PLS_4_SYSTEMMALFUNCTION_FL);
}
}
//...
這只是代碼的一個小例子。大約有 300 多行這樣繼續下去。
現在我知道網上已經有很多答案了,我也看過其中的一些,但我不確定在這種情況下哪個答案對我最有幫助。
uj5u.com熱心網友回復:
您可以將其更改為列舉、新型開關等。這當然可以使代碼更具可讀性和可維護性。
但是,您說的是提高性能。更改此代碼很可能對性能幾乎沒有任何影響,至少不會以積極的方式影響。
如果您想提高性能,請測量并找到代碼中的瓶頸并專注于這些。
uj5u.com熱心網友回復:
這可能有幫助!!
if(disturbance2 == 0) {
switch (disturbance1){
case SPS1_20_PUFFER_EMPTY:
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
break;
case SPS1_30_STOPPERFL_LOCKED:
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
break;
case SPS1_40_DISTURBANCEFL:
if (bMessage.getDisturbanceType().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_SHORT) {
checkWarnings(bMessage, PLS_3_SHORTDISTURBANCE_FL);
}
else if (bMeldung.getStoerungsartMde().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_LONG) {
checkWarnings(bMessage, PLS_4_SYSTEMMALFUNCTION_FL);
}
break;
}
}
你也可以寫
if(disturbance2 != 0){
// return or exit if requires
}
// 然后啟動 switch() ,你可以洗掉一個塊的分支
uj5u.com熱心網友回復:
開啟干擾 1 會有所幫助。
查看來自 Java17 的新開關樣式。它使代碼非常精簡。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346365.html
標籤:爪哇
上一篇:Java程式結構
