# HG changeset patch # User Sascha L. Teichmann # Date 1370105992 -7200 # Node ID 073268a137d5ce6ef130c2a0afec1c5505d999cc # Parent 04dd1f598ef99001c84e94d980293a4c57192817 Bed height differences: Some refactoring of the calculation to improve readability and performance. diff -r 04dd1f598ef9 -r 073268a137d5 artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java Sat Jun 01 09:10:15 2013 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java Sat Jun 01 18:59:52 2013 +0200 @@ -114,71 +114,73 @@ private BedDiffEpochResult calculateEpochDifference(BedHeight[] pair) { - TDoubleArrayList stations = pair[0].getStations(); - TDoubleArrayList diffRes = new TDoubleArrayList(); - TDoubleArrayList kms = new TDoubleArrayList(); - TDoubleArrayList heights1 = new TDoubleArrayList(); - TDoubleArrayList heights2 = new TDoubleArrayList(); + BedHeight bh1 = pair[0]; + BedHeight bh2 = pair[1]; - for (int i = 0; i < stations.size(); i++) { - if (!Double.isNaN(pair[0].getHeight(stations.get(i))) && - !Double.isNaN(pair[1].getHeight(stations.get(i)))) { - double hDiff = - pair[0].getHeight(stations.get(i)) - - pair[1].getHeight(stations.get(i)); + TDoubleArrayList stations = bh1.getStations(); + int size = stations.size(); + + TDoubleArrayList diffRes = new TDoubleArrayList(size); + TDoubleArrayList kms = new TDoubleArrayList(size); + TDoubleArrayList heights1 = new TDoubleArrayList(size); + TDoubleArrayList heights2 = new TDoubleArrayList(size); + + for (int i = 0; i < size; i++) { + double station = stations.getQuick(i); + double h1 = bh1.getHeight(station); + double h2 = bh2.getHeight(station); + double hDiff = h1 - h2; + if (!Double.isNaN(hDiff)) { diffRes.add(hDiff); - kms.add(stations.get(i)); - heights1.add(pair[0].getHeight(stations.get(i))); - heights2.add(pair[1].getHeight(stations.get(i))); + kms.add(station); + heights1.add(h1); + heights2.add(h2); } } - Date start = ((BedHeightEpoch)pair[0]).getStart(); - Date end = ((BedHeightEpoch)pair[1]).getEnd(); + Date start = ((BedHeightEpoch)bh1).getStart(); + Date end = ((BedHeightEpoch)bh2).getEnd(); return new BedDiffEpochResult(kms, diffRes, heights1, heights2, start, end); } private BedDiffYearResult calculateYearDifference(BedHeight[] pair) { - TDoubleArrayList stations = pair[0].getStations(); - TDoubleArrayList diffRes = new TDoubleArrayList(); - TDoubleArrayList kms = new TDoubleArrayList(); - TDoubleArrayList morphs = new TDoubleArrayList(); - TDoubleArrayList absolute = new TDoubleArrayList(); - TDoubleArrayList gap = new TDoubleArrayList(); - TDoubleArrayList heights1 = new TDoubleArrayList(); - TDoubleArrayList heights2 = new TDoubleArrayList(); - BedHeightSingle s1 = (BedHeightSingle)pair[0]; BedHeightSingle s2 = (BedHeightSingle)pair[1]; - int range = s1.getYear() - s2.getYear(); - if (range < 0) { - range = range * -1; - } - for (int i = 0; i < stations.size(); i++) { - if (!Double.isNaN(s1.getHeight(stations.get(i))) && - !Double.isNaN(s2.getHeight(stations.get(i)))) { - double hDiff = - s1.getHeight(stations.get(i)) - - s2.getHeight(stations.get(i)); + + TDoubleArrayList stations = s1.getStations(); + int size = stations.size(); + + TDoubleArrayList diffRes = new TDoubleArrayList(size); + TDoubleArrayList kms = new TDoubleArrayList(size); + TDoubleArrayList morphs = new TDoubleArrayList(size); + TDoubleArrayList absolute = new TDoubleArrayList(size); + TDoubleArrayList gap = new TDoubleArrayList(size); + TDoubleArrayList heights1 = new TDoubleArrayList(size); + TDoubleArrayList heights2 = new TDoubleArrayList(size); + + int range = Math.abs(s1.getYear() - s2.getYear()); + + for (int i = 0; i < size; i++) { + double station = stations.getQuick(i); + double h1 = s1.getHeight(station); + double h2 = s2.getHeight(station); + double hDiff = h1 - h2; + + if (!Double.isNaN(hDiff)) { diffRes.add(hDiff); - double km = stations.get(i); - kms.add(km); - if (s1.getMorphWidth(km) > - s2.getMorphWidth(km)) { - morphs.add(s1.getMorphWidth(km)); - } - else { - morphs.add(s2.getMorphWidth(km)); - } - if (s1.getDataGap(km) > s2.getDataGap(km)) { - gap.add(s1.getDataGap(km)); - } - else { - gap.add(s2.getDataGap(km)); - } - absolute.add((hDiff / range) * 100); - heights1.add(s1.getHeight(km)); - heights2.add(s2.getHeight(km)); + kms.add(station); + + morphs.add(Math.max( + s1.getMorphWidth(station), + s2.getMorphWidth(station))); + + gap.add(Math.max( + s1.getDataGap(station), + s2.getDataGap(station))); + + absolute.add((hDiff / range) * 100d); + heights1.add(h1); + heights2.add(h2); } } return new BedDiffYearResult(