This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Inplace modification with return by value functions

Tags: None
(comma "," separated)
hijokpayne
Registered Member
Posts
25
Karma
0
When writing functions with Eigen types, I am currently doing either one of the following styles: pass as reference or return by value.

Code: Select all
template <class Derived>
void in_place_function(MatrixBase<Derived> const& input) {
  MatrixBase<OtherDerived>& result = const_cast< MatrixBase<OtherDerived>& >(C_);
   ///// Do Stuff .......
}

template <class Derived>
typename Derived::PlainObject function(const MatrixBase<Derived>& input) {
  typename Derived::PlainObject result(input.rows(), input.cols());
   ///// Do Stuff .......
  return result; // Should be a NRVO
}

Now we can call these functions like:

Code: Select all
MatrixXd input(3, 100000);
...
// (1) in place Transform
in_place_function(input);

// (2) Create a new output
MatrixXd result = function(input);

// (3) Expecting Inplace transformation like (1) (but much slower)
input = function(input);

I prefer doing return by value for output. I get the desired result with the above function, but when I want to do inplace transformation by writing like (3), the performance is quite worse than (1). Is it possible to achieve the same performance of in_place_function with function ? If yes how should we then write function?


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]