3

In a previous program that I have written in C I needed a single object with several "core" data in it that can be accessed by all the functions in my program, I end up picking a struct and i have used a pointer to this struct for reading or writing data; it was fast and good for the job, also it was cheap because accessing a pointer is probably one of the cheapest thing that you can do in C and I have never found something better so I'm happy with this solution.

Now in C++ I have the same problem, I need to share a state composed of some primitive types, I'm tempted to use one of the so called POD, which basically mean, struct, again, but this time with references for safety.

Supposing that I need this "Blob" of data to be carried around my program, a struct accessed by reference is the fastest thing in C++? How much a getter methods can cost?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
axis
  • 874
  • 2
  • 7
  • 13
  • There should be no perceivable speed difference in accessing the data through the pointer and the reference: they are after all the same. – Vlad Oct 06 '12 at 12:10
  • An `inline` getter will also have absolutely no overhead. – enobayram Oct 06 '12 at 12:11
  • If your data is global and unique, maybe you just need a globally accessible singleton? (This might make TDD more complicated, so be warned.) – Vlad Oct 06 '12 at 12:11
  • @Vlad yep, no discussion on that, but i think that references are a little bit more friendly – axis Oct 06 '12 at 12:11
  • @axis: reference instead of pointer is a more "C++-way", if you never plan really having NULL pointer. – Vlad Oct 06 '12 at 12:12
  • @enobayram good thinking, the inline keyword can be good for this ... – axis Oct 06 '12 at 12:13
  • @Vlad you mean the Singleton Design Pattern ? – axis Oct 06 '12 at 12:15
  • 1
    @axis: actually, `inline` keyword is just a hint for compiler that the function might be inlineable (but not only -- for purists); the major modern compilers are very good in inlining functions without explicit hints. – Vlad Oct 06 '12 at 12:17
  • @axis: yes, singleton pattern. This way you don't need to pass the reference along all the way. – Vlad Oct 06 '12 at 12:17
  • @Vlad well, `inline` is not just a hint, it also allows you to define non-template functions in header files. – enobayram Oct 06 '12 at 12:19
  • @enobayram: I know (http://stackoverflow.com/a/3212635/276994), that's why I've put a note for purists :) – Vlad Oct 06 '12 at 12:21
  • @Kerrek: I assume the OP wants to be sure that the pointer is not NULL before dereferencing it. Reference would be free from this problem. – Vlad Oct 06 '12 at 12:23
  • 1
    @Vlad There's a lot of [controversy](http://stackoverflow.com/questions/86582/singleton-how-should-it-be-used) around the singleton design pattern, and a definite consensus: people overuse it... So, the question is: Why not a global variable? Actually, if it's not too much trouble, simply passing around a reference to the data is the most future-proof solution. – enobayram Oct 06 '12 at 12:23
  • @Vlad Sorry, I didn't see the note, either you've edited it in after I've seen it, or I need more sleep :) – enobayram Oct 06 '12 at 12:24
  • @enobayram: well, that's what I write: "This might make TDD more complicated, so be warned." -- are there any more problems with singleton pattern? The global variable is nothing but a singleton which is not possibly lazy. – Vlad Oct 06 '12 at 12:24
  • @enobayram: yes, I've added it later, sorry :) – Vlad Oct 06 '12 at 12:24

1 Answers1

2

If your getter code is inline (in the header file), then the compiler can eliminate the need to call a function in the machine code it outputs.

eg:

class Data
{
private:
  int number_;
public:
  int GetNumber() { return number_; }
};

The compiler will see GetNumber's definition, will know what it does is simple and and where you've called GetNumber(), it will simply replace it with number_. So, using a getter versus accessing the member directly will result in the equivalent code, and both will perform the same.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • probably this kind of approach plus the use of references can be really cheap and fast, the problem is that `inline` is more like a suggestion rather than a real keyword with a well defined behaviour, that's why references are my starting point, because at least i know that references will always behave like references, something that i can't say about inline methods. – axis Oct 06 '12 at 12:45
  • If that worries you, the program you're writing must have extremely high speed performance requirements. Eg doing something like dealing with packets of data coming through a router where every wasted cpu instruction counts. If that is the case, you probably should be examining the generated machine code your compiler is producing to ensure it is efficient enough for your application. – Scott Langham Oct 06 '12 at 12:56
  • not really my point, i was referring to a predictable behaviour, i'm interested in speed and performance, but what i can say about references i can't say about inline methods. – axis Oct 06 '12 at 13:46