Sign in to follow this  

Pointer vs return value overhead

This topic is 2814 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

Quote:
Original post by Ectara
Is it more efficient to call a function prototyped as void add(int a, int b, int *result), or int add(int a, int b)?


There's no clear cut answer for that, because it depends on the compiler and what actually happens inside of "add". If you're calling this function a lot, profile.

If you want a guess, I'd go with version 2 because it's easier to read and the better optimization candidate.

If you want to leave the realm of pure C, "void add(int a, int b, int& result)" would also be a candidate to consider and certainly better than the pointer version because you wouldn't have to check for a NULL pointer...

Share this post


Link to post
Share on other sites
For small types there shouldn't be any significant difference -- if anything, I'd agree with the above poster that the former version won't be more efficient than the latter.

For large types, when the reciever object can be re-used, and when you don't have to spend a lot of time clearing old cruft out between uses then it might possibly be more efficient.


However, this is not the type of thing you want to take on until:
a) You know that using the later version is a bottleneck.
b) You know (having tested) that the former version is faster in this case.
c) You know that you can live with the ugly syntax that the former will introduce.


In real-world use, I'd say that the benefit of this type of scheme is rare. You sometimes see it in vector math libraries, but I've never found it necessary to use that form myself. It buys maybe a few percent speedup at best, in the cases that it buys anything at all.

Share this post


Link to post
Share on other sites

This topic is 2814 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this