Archived

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

Will this be faster?

This topic is 5671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Would passing a float by refrence be faster than passing it by value, or is there no difference? For instance: void Function( float &a ) void Function( float a )

Share this post


Link to post
Share on other sites
You have a compiler? You have the code? Why not stick it in and see which one runs faster, rather then ask for peoples personal opinons (Of which some will be wrong or misinformed)?

Share this post


Link to post
Share on other sites
Lets do some constructive thinking here:

If you pass by value, it is usually passed on the stack, yes? A float is usually 4 bytes, mmkay? Good. I thought so.

That brings us to the reference. Now you ask, what is a reference? Nothing but a pointer in disguise, young grasshopper. And we all know that a pointer on a 32 bit architecture is 4 bytes, I hope?

So - it boils down to these two interesting choices:
1 - Pass 4 bytes on the stack
2 - Pass 4 bytes on the stack

Which one do you think is faster?


Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
Now you ask, what is a reference? Nothing but a pointer in disguise, young grasshopper.



With the reference you still pay for the extra indirection : the address _may_ be put in a register, but the value itself ought not to. Of course, it''ll be in the cache, but still..

For basic types, use pass-by-value.
That being said, Michalson is right.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Your compiler probably implements references like pointers, so the float would be faster.

Bare float...
4 bytes onto stack
4 bytes off the stack
Direct access

Pointer...
4 bytes on the stack
4 bytes off the stack
Dereference whenever it''s accessed


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
how could you test something like this?

Share this post


Link to post
Share on other sites
but a real world test is MUCH better. actually implemneting the function in your game, and test both ways would be more benifical. you would be surprised how little difference certain things make, yet how much things change when in real world code. example: in your million interation loop you should realize that not all the registers will be used, nor will their be much gonig on that would show any reason to do things one way vs another. the compiler could very well optimize both methods to put the float directly onto the stack with on indirection (if your code dont modify the valu, its likly it will treat both ways the same, look at the asm output to see).

dont try optimizing little things like this which may save you a cycle or two (hardly notciable, unless you do the function a million times per frame, but then you have other things to worry about liek why you are calling a function a million times per frame). there is no reaon fo a compiler to dereference the float value every time its accessed. its will most likly (again see the asm output) be dereferenced once, then only derefercend again when required (ie value changes and need to update the memory location with the correct value).

in the end, spend time optimizing the actual function. if you want to get this type of optimization, switch to writing your app in asm. you must trust the compiler at some point. atomic types should be pass by value. leave by reference for structs, arrays, objects, or atomic types you need to modify within the function (ie pass a float since the function will change it).

Share this post


Link to post
Share on other sites