|
From: Mark R. D. <mdi...@la...> - 2003-05-23 16:08:02
|
Is it really neccessary to reallocate memory everytime you use
DoubleArray.copyTo? This seems to be expensive to me, especially in the
case of Diffuse2D.
/**
* Copies the double[] in this DoubleMatrix to the specified DoubleMatrix.
*/
public void copyMatrixTo(DoubleMatrix dm) {
double[] aMatrix = new double[sizeY * sizeX];
System.arraycopy(matrix, 0, aMatrix, 0, sizeY * sizeX);
dm.matrix = aMatrix;
}
I think the above method could easily be written as:
public void copyMatrixTo(DoubleMatrix dm) {
System.arraycopy(matrix, 0, dm.matrix , 0, sizeY * sizeX);
}
without having to build a new double array. I understand that the matri
could be different sizes so:
/**
* Copies the double[] in this DoubleMatrix to the specified DoubleMatrix.
*/
public void copyMatrixTo(DoubleMatrix dm) {
if(matix.length != dm.matrix.length){
double[] aMatrix = new double[sizeY * sizeX];
System.arraycopy(matrix, 0, aMatrix, 0, sizeY * sizeX);
dm.matrix = aMatrix;
}else{
System.arraycopy(matrix, 0, dm.matrix , 0, sizeY * sizeX);
}
}
captures that case while still providing efficient copying of
DoubleArrays of equal sizes by reusing allocated memory.
what do you think?
Mark
|