Subj : Re: Matrix Operation To : borland.public.cpp.borlandcpp From : Mohsen Date : Sun Apr 24 2005 01:30 pm maeder@glue.ch (Thomas Maeder [TeamB]) wrote: >maeder@glue.ch (Thomas Maeder [TeamB]) writes: > >> One obvious alternative is an array with rows*columns elements. This >> will certainly speed up both allocation and deallocation. I'm not an >> expert, but the contiguity of all the elements also might have an >> impact on how the matrix can be cached. > >Oh, and you'll also dereference one less pointer per element access. But this has the drawback that in order to access the elements of farthest rows great number of multiplication is necessary. I've wrapped the matrix in a template. I've noticed that the compiler generates a code such as this one mov esi, [esi+ebx*4] for an indexed access to individual elements of an array such as this one: for (unsigned i = 0; i < n; i++) a = array[i] * 2; but this one produces a better code: double* ptr1 = array; double* ptr2 = ptr1 + n; for (; ptr1 < ptr2; ptr1++) a = *ptr1 * 2; This eliminates the multiplication by 4 in previous assembly code and could have a great influence when the dimensions are large. But in matrix case it is inevitable to bypass the problem mentioned above and I thought it could be solved by changing the way we save the matrix. For example accessing the elements in each row is OK, but elements in columns still have the same problem. I don't know whether I could clarify my problem. Mohsen .