Boost logo

Ublas :

From: Dag Lindbo (dag_at_[hidden])
Date: 2007-11-05 05:17:47


Hello everyone (this is my first post here),

I'm looking for some nice way to investigate if the compiler (gnu 4.1.3
in my case) applies return value optimization (RVO) to functions that
return ublas vectors.

Example: A polynomial that I want to evaluate using op() like this:

#include <boost/numeric/ublas/vector.hpp>
typedef boost::numeric::ublas::vector<double> Vector;

int main(void){
  Polynomial p( ... );
  Vector x = ... ;
  Vector y = p(x);
}

The class looks like this:

class Polynomial{
  const Vector coeff;
  const int degree;

public:
  Polynomial(const Vector& c) : coeff(c), degree(c.size()-1){}
 
  Vector operator()(const Vector& x) const {
   
    Vector y(x.size());
    double p;
    for(int i=0;i<x.size();i++){
      p = coeff(degree);
      for(int j=degree-1; j>=0; j--)
        p = p*x(i)+coeff(j);
      y(i) = p;
    }
    return y;
  }
};

Now I would like to see that this op() is equally efficient as doing the
same thing in a function

void eval(const Vector& x, Vector& y_out) const;

I realize this is somewhat a compiler-dependent question, so I'm just
looking for general thoughts on RVO and uBLAS vectors/matrices.

Best regards,
Dag Lindbo (Sweden)