Archived

This topic is now archived and is closed to further replies.

Yohumbus

References in inlined functions

Recommended Posts

Yohumbus    152
I was wondering if references have any speed increase or even decrease on inlined functions. for example is there any difference in Vector Vector::operator+(Vector &Pls){ Vector RetRef(X+Pls.X,Y+Pls.Y,Z+Pls.Z); return RetRef; } and Vector Vector::operator+(Vector Pls){ Vector RetRef(X+Pls.X,Y+Pls.Y,Z+Pls.Z); return RetRef; }

Share this post


Link to post
Share on other sites
n0ob    176
Well, if I get what you're asking, passing a reference to and object instead of a copy of the object would be especially good in cases where the structure is larger than a reference. So pretty much any structure is better passed by reference or pointer. I just pass by value ints and doubles; I'm not sure which is faster in this case. I have even less ideas about inline functions :S
--------------------
Kings of Chaos

[edited by - n0ob on March 17, 2003 11:09:38 PM]

Share this post


Link to post
Share on other sites
RamboBones    102
Not sure what your examples are to do but as far as I can tell that you want to know if references have a speed difference on inlined functions. Well take the following two example functions

struct BIG_STRUCT
{
int a,b,c,d,e,f,g,h,i,j,k,l,m,n;
};

inline int AddStruct(BIG_STRUCT &Struct)
{
//add up all the values and return
}

inline int AddStruct(BIG_STRUCT Struct)
{
//same as before
}

Calling one of these functions would result in the SAME code being generated. So there is not speed differences. However you should note that in all likely hood the AddStruct function won''t be inlined because it''s to big. Therefore the one that uses the reference opp would be faster, in calling time, and at run time.

I have no idea if I even answered what you asked but...

Share this post


Link to post
Share on other sites
daerid    354
quote:
Original post by RamboBones
Calling one of these functions would result in the SAME code being generated.


100% INCORRECT.

In the example with the reference, any code in the function will actually use the memory location of the original structure used when calling the function.

In the other example (non-referenced), a NEW structure will be allocated on the stack and bitwise copied (since there''s no copy ctor).

Granted, there''s likely to be NO performance difference, but be assured that the above functions will produce DIFFERENT code. It''s guaranteed by the semantics of the c++ language.

Share this post


Link to post
Share on other sites
chiuyan    122
daerid wrote
"100% INCORRECT
In the example with the reference, any code in the function will actually use the memory location of the original structure used when calling the function.

In the other example (non-referenced), a NEW structure will be allocated on the stack and bitwise copied (since there''s no copy ctor)."

If both functions are inlined, there is no stack to allocate a new structure on for the function, and both will (should) generate the same code. In the case that the function cannot be inlined, then yes, using the reference is preferable, but the OP asked about inlined functions.


Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by chiuyan
If both functions are inlined, there is no stack to allocate a new structure on for the function, and both will (should) generate the same code. In the case that the function cannot be inlined, then yes, using the reference is preferable, but the OP asked about inlined functions.



Inline functions will still use local stack variables if you pass by value and modify the parameters. Or make copies in registers if it fits. Passing by reference, or better, by const reference alleviates that need.

The fact that you don''t get a stack frame for the inlined function doesn''t mean no stack frame exists : the enclosing function is adjusted to take into account the local variables and parameters of the inline functions it contains.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites