Boost logo

Boost Users :

From: jhr.walter_at_[hidden]
Date: 2003-02-13 16:20:01


----- Original Message -----
From: Ivan Vecerina
To: boost-users_at_[hidden]
Sent: Thursday, February 13, 2003 3:41 PM
Subject: [Boost-Users] Re: UBLAS: copy on write for matrix & vector?

> <speedsnaii_at_[hidden]> wrote in message news:b2g7nj+6gee_at_eGroups.com...
> | A question on use of the matrix and vector classes:
> |
> | Should I:
> ...
> | matrix<double> foo(const matrix<double> a)
> ...
> | void foo(matrix<double>& r, const matrix<double> a)
> ...
> | In other words: is it expensive to construct the temporary in case
> | one? As I understand, the class would need an underlying 'copy on
> | write' to be cheap, similar as strings do.
>
> By default, the copy constructor of boost::numeric::ublas::matrix
> appears to copy its representation in a newly allocated block.
> As far as I can tell, this behavior could be customized by
> using a custom storage type (the third template parameter).

Yup. One could play ugly games with class array_adaptor<> for example, but
I'd tend to advise against it.

> However, many optimizing compilers are able to eliminate
> unnecessary copying of the return value of a function.
> (This is called RVO or NRVO for Return Value Optimization and
> Named..., depending on how the function is written).
> For example, MSVC suppresses the unnecessary copy,
> even in Debug mode with no optimizations enabled.
>
> So I think you should prefer the interface which is more
> natural to use:
> matrix<double> foo(const matrix<double> a)

Generally agreed, although ublas' bench1 shows, what can happen (especially
for small dimensions) if temporaries are involved.

Thanks,

Joerg


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net