Reputation: 139
Assuming we are running a compiled C++ binary:
Is passing around an int
(e.g. function to function, or writing it into variables) slower than passing around structs/class objects like the following?
class myClass
{
int a;
int b;
char c;
vector d;
string e;
}
Upvotes: 4
Views: 312
Reputation: 3986
First, a lot depends upon whether you are passing objects by reference or by value.
In C# and Java, you're always passing objects around by reference, but in C++, you can pass a copy of an object on the stack (i.e. pass-by-value). Doing this involves making a physical copy of the object in memory, which implies a pair of constructor/destructor calls (at least).
Passing an int
can be done either by value or by reference. If you pass by value, you're copying an int
type (let's say 32 bits
) onto the stack. If you pass by pointer, you're copying an address (again, let's say 32 bits
) onto the stack. There really isn't any difference in terms of stack usage. In the pass-by-reference case, the calling function will have to dereference the pointer to get access to the value of the int
parameter, so there is going to be some additional code (and potentially reduced performance).
Passing an object (or struct) by value vs. reference is more interesting, because they may have very different memory footprints (depends upon the size of the class/struct).
Upvotes: 1
Reputation: 58677
Passing an instance of myClass
around is slower than passing an int
variable around, because the class encapsulates more than an int
. Rather, you should ask whether passing around the various primitives constituting the member variables around is slower than passing a single object that wraps them all as one. The answer is no, the two methods should exhibit the same performance. There is no magic in a class: a class just associates data with functions, if you'd try to express this on your own without the C++ facilities you'd end up with the same performance -- or, as it may more like be -- a worse performance than C++ compilers already give you.
That said, C++ allows you to override the assignment operator for a given pair of types, and it also allows you to write your own copy-constructor such that an instance constructs in terms of another instance of the same class. These two are functions you write, and so the performance of the copy will depend on the performance of these functions. If you don't supply your own copy mechanism and you use the one C++ provides then the performance is optimal, in that the copy is done bit-wise. That is, each member simply itself gets copied.
Upvotes: 2
Reputation: 35594
It depends a lot on the members of your class.
Usually you supply a copy constructor, which copies all the needed data to the target instance of your class. The copy constructor is often optimized by the compiler, so there will be no difference between passing a class with 2 int fields, and just passing 2 ints.
Upvotes: 0
Reputation: 175395
Any time something gets copied how long it takes is going to be a direct result of how big that thing is and what things its copy constructor does; obviously that class is larger than a single int, so it would be slower. If you pass a pointer or pass the thing by reference, there's no copy required and it takes the same amount of time
Upvotes: 3
Reputation: 65126
Passing a pointer or reference to an object around is the same as passing an integer around.
However, if you're passing actual objects (not pointers to them) around, you may end up with copies being made of the objects, which is expensive. A lot of the possible copies may get optimized away though, but it still happens.
Upvotes: 2
Reputation: 284836
It depends on several factors, including the complexity of the copy-constructor and whether the compiler can do elision.
Upvotes: 7