So I think I found a solution but I don't understand why it works. If anyone knows, please let me know.
My old code was:
MWCellArray output = new MWCellArray(rows, columns);
for (int i =0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
output[i+1, j+1] = WrapObject<T>(input[i, j]);
}
}
return output;
In this code, T might be MWCellArray and an empty MWCellArray would be returned if the input value is null. I think this was a big part of the problem.
Changing WrapObject to return null and using this to this code speeds it up a little:
MWCellArray output = new MWCellArray(rows, columns);
for (int i =0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
MWArray value = WrapObject<T>(input[i, j]);
if (value != null)
output[i+1, j+1] = value;
}
}
return output;
But this is what actually works and takes probably 5% as long as the original code:
MWArray[,] cache = new MWArray[rows, columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
MWArray value = WrapObject<T>(input[i, j]);
if (value != null)
cache[i, j] = value;
}
}
MWCellArray output = new MWCellArray(rows, columns);
for (int i =0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
MWArray value = cache[i, j];
if (value != null)
output[i+1, j+1] = value;
}
}
Why does building directly on the MWCellArray slow everything down?