performance - Fast access to matrix as jagged array in C# -
i've created lower triangular distance matrix (because of size issues) jagged array note: distances between objects symmetric
var dm = new double[size][] (var = 0; < size; i++) { dm[i] = new double[i+1]; (var j = 0; j < i+1; j++) { dm[i][j] = distance(data[i], data[j]); } }
i need access matrix made following method it
private double getvalueofdm(int row, int column, double[][] dm) { return column <= row ? distancematrix[row][column] : distancematrix[column][row]; }
with visual studio performance analysis 1 sees major speed issue lies in row of getvalueofdm
method.
has idea how speed up?
you remove conditional in method , increase memory usage increase access performance so:
var dm = new double[size][]; (var = 0; < size; i++) { dm[i] = new double[size]; (var j = 0; j < i+1; j++) { dm[i][j] = distance(data[i], data[j]); dm[j][i] = dm[i][j]; } } private double getvalueofdm(int row, int column, double[][] dm) { return dm[row][column]; }
now don't have conditional, compiler can remove branch prediction. also, should run tests actual use cases ensure going problem. analysis reveal branching conditional slowest part of code, doesn't mean it's going slow down noticeably. in addition, try running in release mode (with compiler optimizations) see how affects performance.
if on system don't have memory available double size of array, code have close optimal accessing jagged array.
Comments
Post a Comment