Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-316

Perf improvement: ArrayRealVector makes superfluous copies and often doesn't optimally operate on array values

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0
    • Fix Version/s: 2.1
    • Labels:
      None
    • Environment:

      all

      Description

      As discussed in the mailing list, things like ArrayRealVector#add:

                  double[] out = new double[data.length];
                  for (int i = 0; i < data.length; i++) {
                      out[i] = data[i] + v.getEntry(i);
                  }
                  return new ArrayRealVector(out);
      

      can be improved in the inner loop by simply

                  double[] out = out.clone();
                  for (int i = 0; i < data.length; i++) {
                      out[i] += v.getEntry(i);
                  }
                  return new ArrayRealVector(out);
      

      Which cuts down on array accesses.

      Even more importantly, the last return line should pass in the boolean false, for "shallow copy", or else this whole temporary array is being copied again and then the original discarded.

        return new ArrayRealVector(out, false);
      

        Attachments

        1. MATH-316.patch
          16 kB
          Jake Mannix

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              jake.mannix Jake Mannix
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: