Results 1 to 3 of 3
- 04-04-2011, 11:23 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 44
- Rep Power
- 0
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 ???
XML 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??.Last edited by lulzim; 04-04-2011 at 11:27 PM.
- 04-05-2011, 05:46 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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
...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.Java 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++; } } } }
Java 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? } }Last edited by toadaly; 04-05-2011 at 05:49 AM.
- 04-06-2011, 12:22 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
Java optimization
By Raymond in forum Advanced JavaReplies: 31Last Post: 02-17-2010, 02:40 PM -
Optimization of code
By new_coder in forum Advanced JavaReplies: 4Last Post: 08-17-2009, 03:03 PM -
Optimization of code
By new_coder in forum New To JavaReplies: 1Last Post: 08-16-2009, 09:38 PM -
toHexString optimization (in fact general optimization question)
By jann in forum Advanced JavaReplies: 7Last Post: 12-16-2008, 06:44 PM -
java code optimization
By hey in forum New To JavaReplies: 0Last Post: 02-10-2008, 05:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks