0

As an example, I have a class that uses the Eigen library as follows:

class A
{ 
    private: 
    Eigen::MatrixXd _matrix; 
    public: 
    Eigen::MatrixXd GetMatrix() const; 
}

GetMatrix() as of right now is implemented as such:

Eigen::MatrixXd A::GetMatrix() const 
{
   return _matrix; 
}

Let's say in main.cpp, I have a function that does the following:

void PrintMatrix(const Eigen::MatrixXd &matrix)
{
   std::cout<<"Print matrix: "<<matrix<<std::endl; 
}

int main()
{
     A obj; 
     PrintMatrix(obj.GetMatrix()); 
     return 0; 
}

In the GetMatrix() function, my goal is to return the _matrix value, and it will be used as seen in main(). I would like the most efficient approach.

I am new to optimization and just starting to warm up to pointers. As you guys can see, GetMatrix() currently returns a value, and I am aware that returning by reference and returning by pointer is much faster than by value, but what are the risks/pros and cons of doing either? And which of the two would be preferable for my case?

  • There won't be a difference in terms of performance efficiency. The point is what to do if you can't return a valid value. – πάντα ῥεῖ Dec 02 '20 at 01:01
  • 1
    Have you tried to measure and profile this yourself? For example by doing this a million times? Remembering to enable compiler optimizations for the test program? – Some programmer dude Dec 02 '20 at 01:01
  • 2
    In general: [When to use references vs. pointers](https://stackoverflow.com/questions/7058339/when-to-use-references-vs-pointers) – user4581301 Dec 02 '20 at 01:02
  • Thanks for the swift responses everyone. And @Someprogrammerdude at least for return by value, the actual code I'm working on that does something similar I've measured performance for but only like 10,000 times – sometimesLazy Dec 02 '20 at 01:04
  • 1
    I would point you specifically to [answer currently #2](https://stackoverflow.com/a/7058376) in the question mentioned by user4581301. – JaMiT Dec 02 '20 at 01:05
  • Thanks @JaMiT. Are references and pointers faster than returning by value because it keeps from creating "copies" of the member variable everytime the getter function is called? – sometimesLazy Dec 02 '20 at 01:10
  • 2
    Modern compilers are good at [return-value optimizations and copy elision](https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization). Return by value, and only if it turns out to not pass the requirements you have and that it's a major top-three bottleneck (measured and profiled on an optimized build) you need to bother with explicit and detailed micro-optimizations like that. – Some programmer dude Dec 02 '20 at 01:13
  • 1
    Sometimes your reasoning for being faster applies, sometimes not. You get into some of the same details as are covered by [Passing by Reference or Value](https://stackoverflow.com/questions/64455532). – JaMiT Dec 02 '20 at 01:16
  • Thanks all. This has been very insightful! – sometimesLazy Dec 02 '20 at 01:19

1 Answers1

-1

In your case I would have a method with the signature:

const Eigen::MatrixXd& GetMatrix() const noexecpt;

The compiler will use the most suitable method available. In your case because your print function takes a const reference as a parameter, the reference version will be used automatically.

Nowadays I use the keyword noexcept for public methods. This tells the client that exception handling is not needed. Sometimes the compiler can optimize for that.

Also for optimization prototyping I strongly recommend: https://godbolt.org/ There you also have links to the tools Quick-bench and CppInsights.

And I just need to give you a const reference to a Youtube talk: Jason Turner “Rich Code for Tiny Computers

  • What does adding the additional const do? – sometimesLazy Dec 02 '20 at 01:17
  • 1
    The problem with just *adding* this function is that its signature is exactly the same as the one already shown by the OP. Remember that return type is not part of a functions signature used for overloading. – Some programmer dude Dec 02 '20 at 01:18
  • The additional const acts as a promise: "I the caller shall not modfify this value given to me by reference." Thanks Some programmer dude for the correction. – Rumus Mythenmetz Dec 02 '20 at 01:18
  • Regarding "shall not modfify this reference" it's a little misleading, as a reference itself can't be modified. It's the object *being referenced* that can't be modified. – Some programmer dude Dec 02 '20 at 01:20
  • *"The compiler will use the most suitable method available."* -- based upon the arguments provided, not based upon the return type. – JaMiT Dec 02 '20 at 03:57
  • I believe the question asked for a comparison between returning a reference and returning a pointer. How does returning a pointer fit into your answer? – JaMiT Dec 02 '20 at 03:58
  • *"In your case because your print function takes a const reference as a parameter, the reference version will be used automatically."* -- this is false. The compiler chooses an overload of `GetMatrix()` based upon its arguments, not on where the function is used. – JaMiT Dec 05 '20 at 01:06