adam
adam

Reputation: 685

speed advantage passing fundamental data types by reference c++

Is there any speed advantage in passing fundamental data types (int, float, double, bool) by reference in C++?

Upvotes: 3

Views: 355

Answers (2)

Andrew Rasmussen
Andrew Rasmussen

Reputation: 15099

No.

AFAIK... the way reference works is the compiler will send the memory address of the passed parameter, and at the other side you have a variable of whatever type, with the same memory address as the sent variable. So it is basically the same exact variable, you are just modifying it in a different scope.

The reason it's faster to send a large object is because this means you just have to send the memory address of (or send the pointer to) the object, which is much smaller than copying the entire (large) object back (and possibly forth). However, a pointer is generally around the same size of a fundamental data type, or even larger, so passing it won't save you any speed.

Upvotes: 2

user2100815
user2100815

Reputation:

No, possibly (for naive implementations) the reverse. Non-naive implementations will simply ignore the (presumably const) reference, naive ones (if such exist) will need an additional dereference operation,

Upvotes: 1

Related Questions