Set-Variable Style Functions in C++

  • Context: C/C++ 
  • Thread starter Thread starter neurocomp2003
  • Start date Start date
  • Tags Tags
    C++ Functions
Click For Summary

Discussion Overview

The discussion revolves around the efficiency of two different set-variable style functions in C++ for member variables. Participants explore the implications of using public member variables versus accessor functions in the context of performance, particularly in large-scale simulations.

Discussion Character

  • Technical explanation, Debate/contested, Experimental/applied

Main Points Raised

  • One participant questions the efficiency differences between two member function styles: returning a reference to a member variable versus using a setter function.
  • Another participant suggests that the best way to determine efficiency is to test both approaches through timing or examining generated assembly code.
  • There is a discussion about whether public member variables (like vector._x) are preferable to accessor functions (like vector.x()), with one participant strongly opposing public variables for efficiency reasons.
  • A later reply indicates that testing with a full simulation may not be necessary, proposing that a simpler test program could suffice to evaluate performance.

Areas of Agreement / Disagreement

Participants express differing views on the efficiency of public member variables versus accessor functions, and there is no consensus on the best approach. The discussion remains unresolved regarding the actual performance implications of the different styles.

Contextual Notes

Limitations include the lack of empirical testing results and the dependence on compiler optimizations, which may affect the efficiency of the proposed function styles.

neurocomp2003
Messages
1,359
Reaction score
4
Just curious if there was any difference between the following two set-variable style functions for member variables interms of effiecieny

CType& Member(void) {return _member; } //Used in David Eberly's Code
void Member(CType const& tp) {_member=tp); }

Is there an actual difference?
Also is it better to have public variables for classes that are used constantly like a vector._x or to have them as vector.x()

Heh i should really go to coding forums for this but I've never really used a C/C++ Coding forum before. Maybe i'll go browse gamedev
 
Technology news on Phys.org
The best way to tell is always to try it both ways and check. :smile: You can check either by setting up a test case for timing the difference, or you can look to the generated assembly code for differences.

All of these functions that you mention ought to be inlined... meaning that when you write a function call, the compiler will really replace it with the body of the function, instead of putting a real function call in the compiled code.


Also is it better to have public variables for classes that are used constantly like a vector._x or to have them as vector.x()

Most emphatically no. But I suspect you meant in terms of efficiency, and the answer is the same as above.
 
yeah i could do that =](but i don't know how or have the time...deadlines ugh)...but I'm looking to do large scale sims..and i won't be able to tell the effect(if there is a significant one) until i actually have the sim up and running...at which time i will have already had 200+ files to deal with.
 
Last edited:
You don't necessarily have to test it with the full sim -- just make a toy program that should be sufficient to put it through its paces.
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
6K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K