killme2008/aviatorscript

关于复杂表达式性能优化问题请教

1195952223 opened this issue · 1 comments

计算数据是2560 * 2560大小的float[]
目前测试最慢的地方需要120s左右计算完成,想问一下还可以在那些方面优化,我不需要很高的精度

公式:
(TD == 12 || TD == 13)?(6000.0*0.018*(1-W)*math.sum((5.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_1*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*5.5))),(6.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_2*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*6.5))),(7.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_3*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*7.5))),(8.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_4*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*8.5))),(9.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_5*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*9.5))),(10.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_6*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*10.5))),(11.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_7*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*11.5))),(12.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_8*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*12.5))),(13.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_9*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*13.5))),(14.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_10*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*14.5))),(15.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_11*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*15.5))),(16.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_12*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*16.5))),(17.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_13*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*17.5))),(18.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_14*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*18.5))),(19.5<GDQSFS_1||(Z_1<=0||V_1*1>=1))?(0.0): (T_15*math.exp(-9.208 + 0.018/Z_1 + 1.955 * math.sqrt(0.893*19.5))))): (TD == 21 || TD == 22 || TD == 23||TD == 31 || TD == 32 || TD == 33 || TD == 41 || TD == 42 || TD == 43)?(6000.0*0.018*(1-W)*math.sum(5.5<CDQSFS_1||V_1*1>=1?(0.0): (T_1*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/5.5)),6.5<CDQSFS_1||V_1*1>=1?(0.0): (T_2*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/6.5)),7.5<CDQSFS_1||V_1*1>=1?(0.0): (T_3*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/7.5)),8.5<CDQSFS_1||V_1*1>=1?(0.0): (T_4*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/8.5)),9.5<CDQSFS_1||V_1*1>=1?(0.0): (T_5*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/9.5)),10.5<CDQSFS_1||V_1*1>=1?(0.0): (T_6*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/10.5)),11.5<CDQSFS_1||V_1*1>=1?(0.0): (T_7*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/11.5)),12.5<CDQSFS_1||V_1*1>=1?(0.0): (T_8*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/12.5)),13.5<CDQSFS_1||V_1*1>=1?(0.0): (T_9*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/13.5)),14.5<CDQSFS_1||V_1*1>=1?(0.0): (T_10*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/14.5)),15.5<CDQSFS_1||V_1*1>=1?(0.0): (T_11*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/15.5)),16.5<CDQSFS_1||V_1*1>=1?(0.0): (T_12*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/16.5)),17.5<CDQSFS_1||V_1*1>=1?(0.0): (T_13*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/17.5)),18.5<CDQSFS_1||V_1*1>=1?(0.0): (T_14*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/18.5)),19.5<CDQSFS_1||V_1*1>=1?(0.0): (T_15*math.exp(2.4869 -0.0014*10000*math.pow(1*V_1,2) - 61.3935/19.5)))): (TD == 81 || TD == 82 || TD == 83)?(6000.0*0.018*(1-W)*math.sum(5.5<SDQSFS_1||V_1*1>=1?(0.0): (T_1*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*5.5)/(0.893*5.5))),6.5<SDQSFS_1||V_1*1>=1?(0.0): (T_2*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*6.5)/(0.893*6.5))),7.5<SDQSFS_1||V_1*1>=1?(0.0): (T_3*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*7.5)/(0.893*7.5))),8.5<SDQSFS_1||V_1*1>=1?(0.0): (T_4*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*8.5)/(0.893*8.5))),9.5<SDQSFS_1||V_1*1>=1?(0.0): (T_5*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*9.5)/(0.893*9.5))),10.5<SDQSFS_1||V_1*1>=1?(0.0): (T_6*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*10.5)/(0.893*10.5))),11.5<SDQSFS_1||V_1*1>=1?(0.0): (T_7*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*11.5)/(0.893*11.5))),12.5<SDQSFS_1||V_1*1>=1?(0.0): (T_8*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*12.5)/(0.893*12.5))),13.5<SDQSFS_1||V_1*1>=1?(0.0): (T_9*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*13.5)/(0.893*13.5))),14.5<SDQSFS_1||V_1*1>=1?(0.0): (T_10*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*14.5)/(0.893*14.5))),15.5<SDQSFS_1||V_1*1>=1?(0.0): (T_11*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*15.5)/(0.893*15.5))),16.5<SDQSFS_1||V_1*1>=1?(0.0): (T_12*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*16.5)/(0.893*16.5))),17.5<SDQSFS_1||V_1*1>=1?(0.0): (T_13*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*17.5)/(0.893*17.5))),18.5<SDQSFS_1||V_1*1>=1?(0.0): (T_14*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*18.5)/(0.893*18.5))),19.5<SDQSFS_1||V_1*1>=1?(0.0): (T_15*math.exp(6.1689 - 0.0743*100*1*V_1 - 27.9613*math.log(0.893*19.5)/(0.893*19.5))))):0.0

部分日志
`11:02:25.677 [main] WARN XXXXRasterCalculator - 固定参数:GDQSFS_1 -> 5.5
11:02:25.679 [main] WARN XXXXRasterCalculator - 固定参数:CDQSFS_1 -> 5.5
11:02:25.680 [main] WARN XXXXRasterCalculator - 固定参数:SDQSFS_1 -> 5.5
11:02:38.697 [raster-cal-0-0] DEBUG XXXXRasterCalculator - 读取耗时: 12s
11:02:40.123 [raster-cal-0-0] DEBUG XXXXRasterCalculator - 计算耗时: 1s
11:02:40.391 [raster-cal-0-0] DEBUG XXXXRasterCalculator - 写入耗时: 0s

11:02:43.824 [raster-cal-0-2] DEBUG XXXXRasterCalculator - 读取耗时: 1s
11:02:55.201 [raster-cal-0-2] DEBUG XXXXRasterCalculator - 计算耗时: 11s
11:02:55.244 [raster-cal-0-2] DEBUG XXXXRasterCalculator - 写入耗时: 0s

11:02:57.002 [raster-cal-0-3] DEBUG XXXXRasterCalculator - 读取耗时: 1s
11:03:54.974 [raster-cal-0-3] DEBUG XXXXRasterCalculator - 计算耗时: 57s
11:03:55.044 [raster-cal-0-3] DEBUG XXXXRasterCalculator - 写入耗时: 0s

11:03:56.695 [raster-cal-0-4] DEBUG XXXXRasterCalculator - 读取耗时: 1s
11:05:03.251 [raster-cal-0-4] DEBUG XXXXRasterCalculator - 计算耗时: 66s
11:05:03.315 [raster-cal-0-4] DEBUG XXXXRasterCalculator - 写入耗时: 0s

11:05:43.424 [raster-cal-1-0] DEBUG XXXXRasterCalculator - 读取耗时: 16s
11:05:44.966 [raster-cal-1-0] DEBUG XXXXRasterCalculator - 计算耗时: 1s
11:05:45.046 [raster-cal-1-0] DEBUG XXXXRasterCalculator - 写入耗时: 0s

11:05:45.353 [raster-cal-1-1] DEBUG XXXXRasterCalculator - 读取耗时: 0s
11:05:56.614 [raster-cal-1-1] DEBUG XXXXRasterCalculator - 计算耗时: 11s
11:05:56.662 [raster-cal-1-1] DEBUG XXXXRasterCalculator - 写入耗时: 0s

11:05:56.989 [raster-cal-1-2] DEBUG XXXXRasterCalculator - 读取耗时: 0s
11:07:11.357 [raster-cal-1-2] DEBUG XXXXRasterCalculator - 计算耗时: 74s
11:07:11.451 [raster-cal-1-2] DEBUG XXXXRasterCalculator - 写入耗时: 0s

11:07:11.803 [raster-cal-1-3] DEBUG XXXXRasterCalculator - 读取耗时: 0s
11:08:35.485 [raster-cal-1-3] DEBUG XXXXRasterCalculator - 计算耗时: 83s
11:08:35.606 [raster-cal-1-3] DEBUG XXXXRasterCalculator - 写入耗时: 0s

11:11:20.790 [raster-cal-1-6] DEBUG XXXXRasterCalculator - 读取耗时: 1s
11:11:59.875 [raster-cal-1-6] DEBUG XXXXRasterCalculator - 计算耗时: 39s
11:11:59.901 [raster-cal-1-6] DEBUG XXXXRasterCalculator - 写入耗时: 0s
`

主要代码

int floatArrLength = 2560 * 2560;
         for (int i = 0; i < floatArrLength; i++) {
            Object[] objs = new Object[variableNames.size() * 2];
            boolean isCal = true;
            for (int i1 = 0; i1 < variableNames.size(); i1++) {
                String variableName = variableNames.get(i1);
                if (!isCal) {
                    continue;
                }
                Double noData = noDataMap.get(variableName);
                boolean floatsIsNull = floatMap.get(variableName) == null;
                double v = floatsIsNull ? tifMap.getDouble(variableName) : floatMap.get(variableName)[i];
                if (x == 0 && y == 0) {
                    noData = noData == null ? floatsIsNull ? noDataValue : floatMap.get(variableName)[0] : noData;
                    noDataMap.put(variableName, noData);
                }
                if (!floatsIsNull && v == noData) {
                    isCal = false;
                }
                objs[i1 != 0 ? 2 * i1 : 0] = variableName;
                objs[i1 != 0 ? 2 * i1 + 1 : 1] = v;
            }
            float d = isCal ? Float.parseFloat(compile.execute(compile.newEnv(objs)).toString()) : noDataValue;
            floats[i] = d;
        }

我看表达式中存在大量重复的子表达式,这些是否可以抽取成变量:

let x = ...;
let y = ...;

x +y

避免无谓的重复计算。