Boost logo

Boost Users :

From: Brian Budge (brian.budge_at_[hidden])
Date: 2006-05-08 01:05:20

Hi all -

I just tried out the operators library for the first time today. I
was quite happy with how easily it helped me implement my test class.
Unfortunately, I'm running into some performance issues, in particular
with operator +.

The interesting thing is that the new operator += is actually faster
than my old += by about a factor of 2, but my operator + is slower
than my old + by a factor of nearly 10!

I'm new here, and not really sure how much code is appropriate to post
to the list, so here's a minimal rundown:

Old code:

template<typename real>
Vector3<real> operator + (const Vector3<real> &a, const Vector3<real> &b);

template<typename real>
class Vector3 {
    real m_v[3];
    friend Vector3 operator +<> (const Vector3 &a, const Vector3 &b);

template<typename real>
inline Vector3<real> operator + (const Vector3<real> &a, const
Vector3<real> &b){
    Vector3<real> t;
    t[0] = a[0] + b[0];
    t[1] = a[1] + b[1];
    t[2] = a[2] + b[2];
    return t;

New code (note that the new code is templatized on size, and that my
were of size 3 to match the above code):

template<typename T, unsigned int S>
class vec
    : boost::additive< vec<T, S>
    , boost::multiplicative< vec<T, S>, T
> >
    typedef unsigned int uint;
    typedef const T &const_reference;
    typedef T &reference;

    boost::array<T,S> m_v;

    template<typename U>
    vec &operator += (const vec<U,S> &t){
        for(uint i = 0; i < S; ++i){
            m_v[i] += t.m_v[i];
        return *this;

Any ideas how to increase the performance of the new code here? A
factor of 10 makes it seem like I am just missing something important.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at