References in inlined functions

Started by
5 comments, last by Yohumbus 21 years, 1 month ago
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; }
ASCII stupid question, get a stupid ANSI
Advertisement
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]
I think pretty much the same rules apply for inlined functions as well
Disclaimer: "I am in no way qualified to present advice on any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)"
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...
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.

daerid@gmail.com
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.


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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement