Am 09.07.2010 um 14:31 schrieb Nasos Iliopoulos:
Phillipp,
1. If your question refers to whether you should use iterators, or indexing, it is mostly a matter of taste. The indexing operation i*size_j+j, compiles to only one operation (multiply-add) in any sensible compiler so there is no penalty from using it. In any case for algebraic legacy reasons I prefer indexing over iterators, although iterators may give you better generality and some cleaner assembly code (if you care about that). I think that some tests in the past indicated that indexing was in fact faster than iterators, but I can't justify it atm.
That' a nice comment. I use Visual Studio and G++ for compiling my application. I think I will use iterators, because it create a little bit cleaner code
2. If you refer to the trasversing order:
If your matrix is row_major (default), then the inner loop should be over the columns (i.e. the second index). That is because when the loop is executed, data is transfered in blocks from the main memory to the CPU's local cache and since you have a block there, you want to do all the possible operations on it before you are done with it (and it is send back to the main memory). Now if the matrix is small enough to fit in the cache (say up to 2MB in practice), this will not be a problem.
For testing I have matrix with around 10k rows and 2-20 columns (doube, float or long double values), at the moment I use code like
for(i=0; i < matrix.size1(), ++i) {
blas::outer_prod / inner_prod(ublas::row(matrix, i), ublas::row(data,i));
}
In reality the row size is more than 10k (average 50-80k) and column size is around 60 columns (worst case 1000). I must use only the outer and inner products for row vectors, so I would prefere indexing and row-order matrix.
Thanks
Phil