优化以下代码: // 初始化结果 long coherenceTotal = 0; long effectiveTotal = 0; long completeTotal = 0; long coherenceSuccess = 0; long effectiveSuccess = 0; long completeSuccess = 0; long lastCoherenceTotal = 0; long lastEffectiveTotal = 0; long lastCompleteTotal = 0; long lastCoherenceSuccess = 0; long lastEffectiveSuccess = 0; long lastCompleteSuccess = 0; // 平均结果 for (DqExecuteResult dqExecuteResult : dqExecuteResults) { String ruleName = dqExecuteResult.getRuleName(); int state = dqExecuteResult.getState(); int total = dqExecuteResult.getTotal(); double statisticsValue = dqExecuteResult.getStatisticsValue(); switch (ruleName) { case "(multi_table_accuracy)": coherenceTotal += total; coherenceSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; break; case "(null_check)": completeTotal += total; completeSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; break; default: effectiveTotal += total; effectiveSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; } } // 最新一次结果 for (DqExecuteResult dqExecuteResult : lastDqExecuteResults) { String ruleName = dqExecuteResult.getRuleName(); int state = dqExecuteResult.getState(); int total = dqExecuteResult.getTotal(); double statisticsValue = dqExecuteResult.getStatisticsValue(); switch (ruleName) { case "(multi_table_accuracy)": lastCoherenceTotal += total; lastCoherenceSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; break; case "(null_check)": lastCompleteTotal += total; lastCompleteSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; break; default: lastEffectiveTotal += total; lastEffectiveSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; } }
时间: 2024-01-12 12:02:59 浏览: 65
优化算法的代码
可以对代码进行以下优化:
1. 使用数组或者集合来存储结果,而不是一一定义变量,这样可以减少代码量,并且更易于扩展和维护。
2. 在计算成功数的时候,可以使用三目运算符来判断是否需要减去统计值,这样可以减少重复代码。
3. 可以将相同的代码块提取出来,减少重复代码。
优化后的代码如下:
```
// 初始化结果
long[] total = new long[3];
long[] success = new long[3];
long[] lastTotal = new long[3];
long[] lastSuccess = new long[3];
// 平均结果
for (DqExecuteResult dqExecuteResult : dqExecuteResults) {
String ruleName = dqExecuteResult.getRuleName();
int state = dqExecuteResult.getState();
int index = getIndex(ruleName);
total[index] += dqExecuteResult.getTotal();
success[index] += (state == DqTaskState.FAILURE.getCode()) ? (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())) : dqExecuteResult.getTotal();
}
// 最新一次结果
for (DqExecuteResult dqExecuteResult : lastDqExecuteResults) {
String ruleName = dqExecuteResult.getRuleName();
int state = dqExecuteResult.getState();
int index = getIndex(ruleName);
lastTotal[index] += dqExecuteResult.getTotal();
lastSuccess[index] += (state == DqTaskState.FAILURE.getCode()) ? (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())) : dqExecuteResult.getTotal();
}
// 根据规则名获取下标
private int getIndex(String ruleName) {
switch (ruleName) {
case "(multi_table_accuracy)":
return 0;
case "(null_check)":
return 1;
default:
return 2;
}
}
```
阅读全文