# Need help for optimization of my code?

• 04-05-2011, 12:23 AM
lulzim
Need help for optimization of my code?
Ok guys i have implemented some code for matix multimplication and some other fnctions as isDiagonal() Transpos, Symetric,etc etc , but the problem is that i have to optimise it to achieve benchmark of the requested from our teacher.
My code i have done is taking too much time, do u have any idea gys how can i reduce time ???

Code:

public RMatrix<E> getTranspose() {
// TODO Auto-generated method stub
SquareMatrix<E> a = new SquareMatrix(size);

for(int i= 0; i < size; i++){
for(int j=0; j < size; j++){
a.vals[i][j] = this.vals[j][i];
}
}

return a;

}

public RMatrix<E> getOne() {
// TODO Auto-generated method stub
Ring<E>[][] result = new Ring[size][size];

for(int i= 0; i < this.size; i++){
for(int j=0; j < this.size; j++){
if(i == j){
result[i][j] = this.getElementAt(i, j).getOne();
}
else{
result[i][j] = this.getElementAt(i, j).getNull();
}
}
}

SquareMatrix<E> oneResult = new SquareMatrix<E>(result);
return oneResult;

}

private RMatrix<E> NaiveMult(RMatrix<E> a ,RMatrix<E> e) {
// TODO Auto-generated method stub
Ring<E>[][] result = new Ring[a.size()][a.size()];
Ring<E>[][] multVal = new Ring[a.size()][a.size()];
int counter;

if(!(a.size() == e.size())) throw new NotCompatibleException("Matrices cannot be multiplied!");

for(int i= 0; i < a.size(); i++){
for(int j=0; j < a.size()  ; j++){
counter = 0;
for(int k = 0; k < a.size(); k++){
multVal[i][j] = a.getElementAt(i, k).mult( e.getElementAt(k, j));
if(counter == 0){
result[i][j] = multVal[i][j] ;
counter++;
}else{
result[i][j] = result[i][j].plus((E)multVal[i][j]);
counter++;
}
}
}
}

SquareMatrix<E> multResult = new SquareMatrix<E>(result)  ;
return multResult;
}

public RMatrix<E> plus(RMatrix<E> e) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Ring<E>[][] result = new Ring[size][size];

if(this.size != e.size()) throw new NotCompatibleException("Matrices cannot be added!");

//Id the matrix for add is empty then no need to do calculations

if(e.equals(e.getNull())){ return this.getCopy(); }

for(int i= 0; i < this.size; i = i+1){
for(int j=0; j < this.size; j = j + 1){
result[i][j] =  this.getElementAt(i, j).plus(e.getElementAt(i, j));
//                                result[i+1][j+1] =  this.getElementAt(i+1, j+1).plus(e.getElementAt(i+1, j+1));
//                                result[i+2][j+2] =  this.getElementAt(i+2, j+2).plus(e.getElementAt(i+2, j+2));
//                                result[i+3][j+3] =  this.getElementAt(i+3, j+3).plus(e.getElementAt(i+3, j+3));

//                                result[i+4][j+4] =  this.getElementAt(i+4, j+4).plus(e.getElementAt(i+4, j+4));
//                                result[i+5][j+5] =  this.getElementAt(i+5, j+5).plus(e.getElementAt(i+5, j+5));
//                                result[i+6][j+6] =  this.getElementAt(i+6, j+6).plus(e.getElementAt(i+6, j+6));
//                                result[i+7][j+7] =  this.getElementAt(i+7, j+7).plus(e.getElementAt(i+7, j+7));
//
}
}

SquareMatrix<E> plusResult = new SquareMatrix<E>(result)  ;

return plusResult;

So any help from u will safe my life , at least where can i optimise the code??.
• 04-05-2011, 06:46 AM
Some ideas for optimization that might not help at all, but potentially could:

- down inside the 'plus' method, you are doing a check to validate sizes. Although this is good practice, if you want performance, do that check one time outside the loops calling 'plus' rather than inside the triple nested loop

- in the places where you have loops of the form 'for(int i=0; i<a.size(); i++)', if you know a.size() is effectively constant during the loop, then change these to something like 'for(int i=0, iEnd=a.size(); i<iEnd; i++)'

- Try moving loop variable declarations outside the loops for the nested loops.

- Dig into 'getElementAt' and make sure it it's simple enough for the compiler to optimize as inline. You could try inlining it if necessary by getting a 2d array outside the loop, and indexing into it directly rather than calling 'getElementAt'.

In this section of code
Code:

for(int i= 0; i < a.size(); i++){
for(int j=0; j < a.size()  ; j++){
counter = 0;
for(int k = 0; k < a.size(); k++){
multVal[i][j] = a.getElementAt(i, k).mult( e.getElementAt(k, j));
if(counter == 0){
result[i][j] = multVal[i][j] ;
counter++;
}else{
result[i][j] = result[i][j].plus((E)multVal[i][j]);
counter++;
}
}
}
}

...there is no reason to have the 'if(counter == 0)' inside the innermost loop. You can elevate it outside the loop, and have two loops instead. Thsi will save a lot of needless condition checking.

Code:

for(int i= 0; i < a.size(); i++){
for(int j=0; j < a.size()  ; j++){
counter = 0;
//check for degenerate case outsize the loops
multVal[i][j] = a.getElementAt(i, 0).mult( e.getElementAt(0, j));
result[i][j] = multVal[i][j] ;
for(int k = 1; k < a.size(); k++){
result[i][j] = result[i][j].plus((E)multVal[i][j]);
}
counter = a.size(); ///calculating counter seems to be a total waste...can you just get rid of it?
}
}

• 04-06-2011, 01:22 AM
lulzim
Thanks a lot some sec are less now :d.