Matrix methods performance confusion. HELP! :)
Hi everybody,
I have two very very similar patches, in which a matrix with random values is transferred to Java-land, and, without doing anything on it, it is sent back to max-land. What is different is only the strategy used in the Java code.
In the first case I used the method copyVectorToArray() triggered by the bang referencing existing named matrices, while in the second case I used the matrix name method as in the example "jitvecprocess-exemple" to refer a matrix connected straight into the mxj inlet.
The rest is pretty similar, but the discrepancy in terms of computation is enormous (~14fps in the first vs 60fps in the second).
Can anybody explain why the second technique is SO much better and/or what have I done horribly wrong in the first example?
I really need to understand this to be able to use the bridge java-max in a sensible way, as so far I feel like I am running around in circle...
I enclose a picture of the super simple patches and both codes.
Thanks everybody for the help!
Example 1:
import com.cycling74.max.*;
import com.cycling74.jitter.*;
public class InOutExperiments extends MaxObject {
JitterMatrix input;
JitterMatrix output;
double[] array;
int len;
int planeSize;
int planes;
int[] size;
int[] offset;
public InOutExperiments(String inputMatrix, String outputMatrix, int width, int height){
declareInlets(new int[]{DataTypes.ALL});
declareOutlets(new int[]{DataTypes.ALL});
input = new JitterMatrix(inputMatrix);
output = new JitterMatrix(outputMatrix, 2, "float32", width, height);
planes = input.getPlanecount(); //getMatrix
size = input.getDim();
planeSize = size[0]*size[1];
len = planeSize*planes;
offset = new int[]{0, 0};
array = new double[len];
}
public void bang() {
for (int i=0; i<size[1]; i++) {
offset[1] = i;
input.copyVectorToArray(0, offset, array, array.length, 0);
//do smtg here!
output.copyArrayToVector(0, offset, array, array.length, 0);
}
}
}
Example 2:
import com.cycling74.max.*;
import com.cycling74.jitter.*;
public class InOutExperiments2 extends MaxObject
{
JitterMatrix mat;
JitterMatrix inmat;
float[] vec;
public InOutExperiments2(Atom[] args)
{
declareInlets(new int[]{DataTypes.ALL});
declareOutlets(new int[]{DataTypes.ALL});
mat = new JitterMatrix();
}
public void bang()
{
outlet(0,"jit_matrix", mat.getAttr("name"));
}
public void jit_matrix(String inname)
{
int[] dim;
int[] offset = new int[2];
int width;
int height;
int planecount;
int lineScan;
inmat = new JitterMatrix(inname);
mat.setinfo(inname);
dim = mat.getDim();
planecount = mat.getPlanecount();
if ((vec==null)||(vec.length!=(dim[0]*planecount)))
vec = new float[dim[0]*planecount];
width = dim[0];
height = dim[1];
offset[0] = 0;
for (lineScan=0;lineScan<height;lineScan++) {
offset[1] = lineScan;
inmat.copyVectorToArray(0,offset,vec,vec.length,0);
// make smtg here
mat.copyArrayToVector(0,offset,vec,vec.length,0);
}
outlet(0,"jit_matrix", mat.getAttr("name"));
}
}