commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Phil Steitz" <>
Subject Re: [math] speeding up RealMatrix.getEntry
Date Mon, 26 May 2008 04:02:41 GMT
On Sun, May 25, 2008 at 8:27 AM, Luc Maisonobe <> wrote:
> Hello,
> Someone complained recently to me about poor performances of
> RealMatrix.multiply(). A quick check showed me that changing the indices
> checks in RealMatrix.getEntry() tremendously improved the performances
> (about a 3 to 1 ratio in a simple case).
> The current implementation explicitely calls the isValidCoordinate prior
> to getting the element from the underlying array. This method calls
> getRowDimension and getColumnDimension and do the intervals checks. I
> think this is unnecessary and duplicates the checks that are already
> done by the JVM. I would like to replace this by an a posteriori check
> when the JVM triggers an ArrayIndexOutOfBoundException. Basically, this
> means replacing:
>  if (!isValidCoordinate(row,column)) {
>    throw new MatrixIndexException(...);
>  }
>  return data[row][column];
> by:
>  try {
>      return data[row][column];
>  } catch (ArrayIndexOutOfBoundsException e) {
>    throw new MatrixIndexException(...);
>  }
> I have read somewhere that array index checking is now highly optimized
> by JVM and can even be automatically removed in some cases. Do you think
> moving to such a way to handle index errors is sensible ? There are
> several other places where this could be done, so I would like to have
> your comments before doing the changes.

+1 for optimizing getEntry as you describe.

Even greater performance improvement in multiply might be obtained by
using direct array references to the multiplicand as we do for the
multiplier when the multiplicand is a RealMatrixImpl.  Its ugly since
getDataRef is not in the RealMatrix interface (since that really
depends on the underlying store being an array), but we could leverage
it in one of two sort of ugly ways.  One would be to just add an
another multiply method taking a RealMatricImpl as actual paramter and
then grabbing and using the data reference.  The other would be to add
an instanceof check in the current multiply and branch the inner loop
to use direct array references if available.

This might make sense for other arithmetic methods as well.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message