Which is better? Returning reference or value?
-
Hey folks, i was just wondering, what do you think which of these would generate a faster way -if there's a difference at all- to query a class variable:
//--- Method A ----
class CMyClass
{
protected:
int m_myVar;public:
int GetVar() const { return m_myVar; }
};//---- Method B ----
...
const int &GetVar() const { return m_myVar; }
...//--- Method C ----
,,,
inline int GetVar() const { return m_myVar; }
...//---- Method D ----
...
inline const int &GetVar() const { return m_myVar; }
...> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
-
Hey folks, i was just wondering, what do you think which of these would generate a faster way -if there's a difference at all- to query a class variable:
//--- Method A ----
class CMyClass
{
protected:
int m_myVar;public:
int GetVar() const { return m_myVar; }
};//---- Method B ----
...
const int &GetVar() const { return m_myVar; }
...//--- Method C ----
,,,
inline int GetVar() const { return m_myVar; }
...//---- Method D ----
...
inline const int &GetVar() const { return m_myVar; }
...> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
In your example you are returning a basic datatype (an integer), then it's quite the same to return by reference or by value: for example on a 32 bit environment, returning by reference means load the address of
m_myVar
inside theeax
register, while returning by value means load the content ofm_myVar
inside that register. The inlined versions of your methods could potentially be better, because the compiler could better optimize the usage of CPU registers and produce a code a bit more efficient. If your methods return aclass
or astruct
the things could be different: returning by value means that your methods should allocate a temporary object on the stack and call its copy constructor. Then returning by reference in most of cases is better. -
Hey folks, i was just wondering, what do you think which of these would generate a faster way -if there's a difference at all- to query a class variable:
//--- Method A ----
class CMyClass
{
protected:
int m_myVar;public:
int GetVar() const { return m_myVar; }
};//---- Method B ----
...
const int &GetVar() const { return m_myVar; }
...//--- Method C ----
,,,
inline int GetVar() const { return m_myVar; }
...//---- Method D ----
...
inline const int &GetVar() const { return m_myVar; }
...> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
Actually in the long run, return by value may be shorter, but it depends on what you are measuring. For basic data types, the actual return doesn't make much difference, but then when you go to use the returned value, if it is returned by reference, there is an extra level of indirection to resolve. Of course what the compiler optimizes out and what else you might do with the results enters into the overall performance, so it's not a simple matter to determine a priori. However, in most cases, I doubt the difference is major.
CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
In your example you are returning a basic datatype (an integer), then it's quite the same to return by reference or by value: for example on a 32 bit environment, returning by reference means load the address of
m_myVar
inside theeax
register, while returning by value means load the content ofm_myVar
inside that register. The inlined versions of your methods could potentially be better, because the compiler could better optimize the usage of CPU registers and produce a code a bit more efficient. If your methods return aclass
or astruct
the things could be different: returning by value means that your methods should allocate a temporary object on the stack and call its copy constructor. Then returning by reference in most of cases is better.Of course with more complex types it's usually better to go by reference, i was just wondering about the basic types indeed. Thank you for your answer. :thumbsup:
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
-
Actually in the long run, return by value may be shorter, but it depends on what you are measuring. For basic data types, the actual return doesn't make much difference, but then when you go to use the returned value, if it is returned by reference, there is an extra level of indirection to resolve. Of course what the compiler optimizes out and what else you might do with the results enters into the overall performance, so it's not a simple matter to determine a priori. However, in most cases, I doubt the difference is major.
CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
Thanks for the info.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <