Originally Posted by

**Harleen**
But I'm really interested in the comparison between the for and while loop, when you apply this change to them.

Sorry for taking so long to make the changes and post an update. Anyways, I made the changes to the multiply methods and also attempted to eliminate any caching/paging optimizations by declaring 2 new matrices before each multiply and deleting them afterwards.

The 2 versions of the matrix multiply now look like:

Code:

Matrix<T> mult_FOR(const Matrix<T>& rhs) const{
assert(nCol_ == rhs.size1() );
const size_t rhs_size2 = rhs.size2();
Matrix<T> theMatrix(nRow_, rhs_size2);
size_t dataOffset = 0;
T *val;
for (int r=0; r < nRow_; ++r) {
for (int c=0 ; c < rhs_size2; ++c) {
T& x = theMatrix(r, c);
val = &data_[dataOffset];
for (int i=0; i < nCol_; ++i) {
x += (*val++) * rhs(i,c);
}
}
dataOffset += nCol_;
}
return theMatrix;
}
Matrix<T> mult_WHILE(const Matrix<T>& rhs) const{
assert(nCol_ == rhs.size1() );
size_t rhs_size2 = rhs.size2();
Matrix<T> theMatrix(nRow_, rhs_size2);
T *val;
int r, c, i;
r = nRow_ -1;
int dataOffset = nCol_*r + nCol_ -1;
do{
c = rhs_size2 -1;
do{
T& x = theMatrix(r, c);
val = &data_[dataOffset];
i = nCol_ -1;
do{
x += (*val--) * rhs(i,c);
--i;
} while(i >= 0);
--c;
} while (c >= 0);
dataOffset -= nCol_;
--r;
} while(r >=0);
return theMatrix;
}

The for loop doing the timing/testing looks like this now:

Code:

for(int i = 0; i < N; ++i){
Matrix<double> *M1 = new Matrix<double>(n1, n2);
Matrix<double> *M2 = new Matrix<double>(n2, n3);
start = clock();
M1->mult_WHILE(*M2);
finish = clock();
time = ((double)(finish - start))/CLOCKS_PER_SEC;
ave_time_WHILE += time;
delete M1;
delete M2;
Matrix<double> *M3 = new Matrix<double>(n1, n2);
Matrix<double> *M4 = new Matrix<double>(n2, n3);
start = clock();
M3->mult_FOR(*M4);
finish = clock();
time = ((double)(finish - start))/CLOCKS_PER_SEC;
ave_time_FOR += time;
delete M3;
delete M4;
}

The newing and deleting seems to remove the speed ups from caching and paging, and now the methods appear to run about the same.

And for the results:

Code:

./a.out
Average time for mult_WHILE (seconds): 1.4265
Average time for mult_FOR (seconds): 1.384

Or, if you feel like bigger matrices and waiting forever for a result:

Code:

Average time for mult_WHILE (seconds): 10.272
Average time for mult_FOR (seconds): 10.2855

In either case, it seems without the performance boosts from the caching, both methods appear to take just as long now. The entire code is attached here:Post3.txt

## Bookmarks