
Ublas : 
From: Jesse Manning (manning.jesse_at_[hidden])
Date: 20071105 08:47:06
Most modern compilers that I know of implement RVO, but a google
search will probably result in a list of compilers that support it.
The example you posted is actually an example of NRVO (named return
value optimization) and not simply RVO. The difference comes in
because RVO implies a temporary object can be avoided by simply
putting it straight into the memory location of the returned value.
With NRVO you actually create a nontemporary object which as far as I
know is not as widely supported as plain RVO.
In the general case RVO would look like this:
class MyClass
{
private:
int x;
public:
MyClass(int y) : x(y) { }
MyClass RVO(int y);
MyClass NRVO(int z);
};
MyClass MyClass::RVO(int y)
{
// candidate for RVO
return MyClass(y);
}
MyClass MyClass::NRVO(int z)
{
// candidate for NRVO
MyClass m(z);
return m;
}
With all of this said I think in the case of a uBLAS vector simply
passing the output vector in by reference is your best bet if you want
to ensure efficiency.
Hope this helps.
Jesse
On Nov 5, 2007 5:17 AM, Dag Lindbo <dag_at_[hidden]> wrote:
> 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=degree1; 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 compilerdependent question, so I'm just
> looking for general thoughts on RVO and uBLAS vectors/matrices.
>
> Best regards,
> Dag Lindbo (Sweden)
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
>