Boost logo

Boost Users :

From: Ivan Vecerina (yg-boost-users_at_[hidden])
Date: 2003-02-17 13:21:48


<speedsnaii_at_[hidden]> wrote in message news:b2nq2e+jht8_at_eGroups.com...
| I tried to find out what is happening in the case:
|
| matrix<double> foo()
| {
| matrix<double> r(3);
| return r;
| }
...
| r1 = foo();
|
| The matrix (that was constructed on the stack) is copied twice when
| used in this manner. One copy for the returned temporary, a second
| copy for the assignment. (I am using MSVC debug mode) And this
| definitely is not a cheap operation. Both in terms of memory _and_
| execution time.

The first copy indicates that MSVC (6?) could not apply the NRVO
optimization. It may support the RVO optimization though: you could
try using direct returns when possible: return matrix<double>(3);

The second copy will be avoided by the compiler if you use
construction of a new variable instead of assignment:
  matrix<double> r1 = foo(); // shall avoid 2nd copy
or:
  matrix<double> const& r2 = foo(); // always optimized
                                    // but can trigger a VC7 bug

I agree these alternatives are not fully equivalent, but they
are coding style tricks that can be useful to improve performance.

| Also a second question comes to me now:
| Can I define a LHS that can be the target of an arbitrary size matrix?
| I.e.:
| matrix<double> lhs;
| matrix<doubel> rhs(3,3);
|
| lhs = rhs;
|
| will give a runtime exeception because of different size. Can I do
| anything about this?

I'm not a UBLAS expert. But here also, declaring variables where
they are initialized would solve the problem, and provide best
performance:
  matrix<double> const lhs = rhs; // drop the 'const' if required

This is the usually recommended C++ style. And to go back to your
original question, it requires that toutines *return* their result
instead of taking output parameters. ( but I cannot certify that
boost's UBLAS was developed with this in mind )

| I have the feeling, that I still do not well understand the overall
| semantics of the matrix and vector classes. Are there any
| books/examples which I can study to get a better understanding?

I won't be of much help here. I admit I don't use UBLAS personally.
( I use small fixed-size matrices in my current applications ).
But, I home some of the suggestions above may still help...
Ivan

--
 Ivan Vecerina, Dr. med.  <>  http://www.post1.com/~ivec
 Soft Dev Manger, xitact  <>  http://www.xitact.com
 Brainbench MVP for C++   <>  http://www.brainbench.com

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