2

I'm writing a neuron network simulation program and every operation or additional byte per neuron scales insanely. I prefer C++ as a language over the others, but now I'm wondering if the class structure takes additional space or resources after compilation in comparison to C for example. If I have a :

class Neuron{
float some,variables,foo,bar;
//And some functions to compute the changes
void foo();
void bar();
}

will it be leaner to have a :

struct Neuron{
float some,variables,foo,bar;
}
void foo(Neuron*);
void bar(Neuron*);

or is it the same after compilation?

  • 4
    Virtual methods will have an extra level of indirection since they have to first jump to the vtable and find the function to call. Non-virtual methods should compile down to the same thing. – Doval Oct 29 '14 at 11:43
  • 2
    Also this may help: stackoverflow.com/questions/422830/… (tl;dr: The C++ standard guarantees that memory layouts of a C struct and a C++ class will be identical as long as the C++ class/struct fits the criteria of being POD ("Plain Old Data")) – thorsten müller Oct 29 '14 at 11:47
  • possible duplicate of Class Versus Struct – gnat Oct 29 '14 at 12:03
  • 1
    Even if there was a difference between those two constructs, it is probably neglegtable for your case (as it is for 99,9% of all real world cases), and believe me, you don't want to use such minor differences as base for your design decisions. I guess you never heard the term "premature optimization"? en.wikipedia.org/wiki/Program_optimization#When_to_optimize – Doc Brown Oct 29 '14 at 12:23
  • Thanks for the replies. The term Doc Brown gave is very applicable in this case :D – petko10 Oct 29 '14 at 13:20
6

If you don't use virtual functions, then there should be no (negative) performance or space impact on using C++ classes when comparing to C structs. The two snippets you gave as example should compile to (nearly) the same assembly code.1

If you add virtual functions to your classes, then there will be a slight memory overhead per object (about the size of a pointer) and a time overhead per call to a virtual function, because the call has to go through a dispatch table. It will also be an indirect jump, which might be slightly more costly on some processor architectures (in the order of a few clock cycles).

Note 1: The compiler may use a different method for passing the implicit this parameter than what is used for the other parameters, but that difference is in the order of choosing register A instead of B or passing in a register instead of on the stack.

-2

If I remembr well all global will be write on http://www.geeksforgeeks.org/memory-layout-of-c-program/ on Uninitialized Data Segment.

Class function member section will be exist only if you have create new instance of the class. So It wil be store in heap.

So i think Global function is faster than Class member function.

To me choose a address of global is faster than ask a first address memory (object) for look if it contains another address memory (member).

  • 1
    OP isn't suggesting globals, just POD (trivially-initialized) struct with free functions, versus class instance with methods. – Useless Oct 29 '14 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.