Reputation: 715
I have a C struct with associated functions:
struct StructC {
int a;
int b;
};
static inline StructC func1()
{
StructC ret;
//do something with variables ret.a and ret.b
return ret;
}
static inline void func2(StructC var)
{
//do something with variables var.a and var.b
...
}
and a C++ struct:
struct StructCPP {
int a;
int b;
StructCPP func3() //can also be static
{
StructCPP ret;
//do something with instance variables ret.a and ret.b
return ret;
}
void func4() //can't use static here since it operates on the instance variables
{
//do something with instance variables a and b
...
}
};
My question: Which is faster when passing these structs to functions/methods?
Since the C functions operating on StructC are static, only one instance reside in memory, but what happens with the C++ methods in its struct?
Do its methods (func3() and func4()) occupy redundant memory for every instance, or does the C++ compiler optimize it to hold only one instance, so while passing the C++ struct, only the instance variables, a and b, are passed?
which function call to these functions is faster (if there is any difference)?:
void functionA(StructC var); //StructC as argument
void functionB(StructCPP var); //StructCPP as argument
(The program is a mixture of C and C++)
Upvotes: 0
Views: 4564
Reputation: 254431
Which is faster when passing these structs to functions/methods?
A member function should be exactly as fast to call as a non-member taking a pointer as an argument; since that's exactly what a (non-virtual, non-static) member function is.
The first non-member function is probably slightly faster to call than the first member, since it doesn't take a hidden this
parameter. However,it doesn't access the object it's called on, so it could be static or a non-member; in which case it will be exactly as fast as the non-member.
The second takes its hidden parameter as a pointer, so it might be slower or faster than the non-member function taking a value, depending on exactly what it's doing with it.
Since the C structs are static, only one instance reside in memory, but what happens with the C++ structs?
C structs aren't static. You can create and destroy them just like any other object - as your example does when it creates a local variable, then returns a copy of it. C++ classes are just the same in that regard.
Do its methods (func3() and func4()) occupy redundant memory for every instance
No, member functions don't occupy any memory in the class instance. Like non-member functions, the code exists in just one place; the only real difference is that member functions are passed an extra argument.
If the class has virtual functions, then that (typically) adds a single pointer, the vptr, to each object, along with a single static table of function pointers and other runtime type information (the vtable) for the class.
while passing the C++ struct, only the instance variables, a and b, are passed?
Indeed. This is a standard layout class, which means that the only things it contains are its data members, just like a C struct.
which function call to these functions is faster (if there is any difference)?
They should be the same; both pass a trivially copyable object containing the same data members by value.
Upvotes: 3
Reputation: 9801
The only thing that you can say for sure, considering the language specs, is the following: struct
, class
and union
are the only 3 class types in C++, that's where the similarity between struct
and class
comes from.
For everything else you should profile, debug and compare different implementations and compilers, after all C++
is still a compiled language, and not everything can be said about a given program if you don't compile it.
Upvotes: 0
Reputation: 437336
Code for methods of a C++ class
or struct
(it's exactly the same) is only included once in your executable file and only present once in memory. It doesn't matter how many objects you create or how many times you call them¹.
The only difference between a method and a free function is that the method gets an additional "hidden" argument in the form of this
. So not even the instance variables are passed individually.
¹ Declaring methods inline
(or equivalently, defining them inside the class definition) might result in multiple copies of the same code being included in your final executable. This behavior is in general very compiler-specific, and in any case also applies to inline
free functions.
Upvotes: 2