Boost logo

Ublas :

From: Max Weinberg (max.weinberg_at_[hidden])
Date: 2005-06-01 15:24:21


Hi,
using Stefan's [corrected] code, I also see that valarray is up to two times
faster than ublas vector. It is hard to say where the inefficiency comes from.
I think it has to do with indirect adressing. ublas uses indexed access when
doing the vector addition. On each element access, first the base address data
() of the vector is fetched, then the index[] is added.
When using the code variant below, you can get faster than valarray. [if the
code is correct, not thoroughly checked]. It uses sequential access to vector
elements. For unknown reasons, valarray also seems to use indexed access (at
least in the VC implementation).
Bye, Max

boost::numeric::ublas::vector<RealType> tmp = v;
const boost::numeric::ublas::vector<RealType>::iterator tmpbegin = tmp.begin();
const boost::numeric::ublas::vector<RealType>::iterator tmpend = tmp.end();
const boost::numeric::ublas::vector<RealType>::iterator vbegin = v.begin();
boost::numeric::ublas::vector<RealType>::iterator j1, j2;
for ( int i = 0; i < iterations; ++i)
{
        for (j1 = tmpbegin, j2 = vbegin; j1!=tmpend; ++j1, ++j2)
        {
                *j1+=*j2;
        }
        for (j1 = tmpbegin, j2 = vbegin; j1!=tmpend; ++j1, ++j2)
        {
                *j1-=*j2;
        }
}