views:

93

answers:

3

For objects or primitive data with a size equal to or less than size of a pointer the most efficient passing method to a function would definitely be by value but the thing is I want to know would be there any tool capable of determining best method of passing objects of classes or primitive data with a sizes bigger than size of a pointer to functions on a platform, like something from boost tools ?

+7  A: 

Yes, Boost has that, called call traits. But its high cost in unreadability is higher than the microscopic gain in efficiency. In my humble opinion.

Cheers & hth.,

Alf P. Steinbach
Thanks for the link. Now I know what "best method" means :-) .
Prasoon Saurav
+5  A: 

Unless you're in a critical section, it's astronomically unlikely pointer dereferencing is the bottleneck of your application, and if you are in critical section, you'd better look at cache friendly algorithms, so that you won't need to pay the cost of dereferencing by much.

In short, pass anything bigger than pointer size by reference/pointer, and only care about performance when you know that you need to care. Premature optimization is the root of all evil.

Lie Ryan
+1 for "Premature optimization is the root of all evil."
JoshD
"Premature optimization is the root of all evil." That's why I started this thread! ;)
Pooria
+2  A: 

Yes, that tool is the compiler. Pass by reference and if the optimizer notices it can get away with storing a copy of the object instead of the address for objects whose size is less or equal to that of an address, then it may do so. This is probably the best approach for templated code, where you can't be sure if your parameters are large or small objects - just use references anyway.

The compiler is not guaranteed or required to make this optimization, but if it doesn't, it's probably a negligable performance hit anyway, so there's no need to worry.

AshleysBrain
It can work the other way too: If you pass by value, the compiler may also be able to perform copy elision, eliminating the copy. So either way, the compiler will try to make your code go fast. :)
jalf
Good point, didn't realise that - but the cost of the optimisation not happening is higher that way round. If a very large object is passed by value, there could be a serious performance hit, but if a small object is passed by reference, it's probably nothing. So I'd prefer pass-by-reference anyway.
AshleysBrain
@AshleysBrain: There are a few scenarios where you'll definitely want to pass by value: http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/
Steve M
@AshleysBrain , jalf_The reference to constant parameter could be optimized to constant value in some circumstances that's been covered by me with another thread already(check it out_http://stackoverflow.com/questions/3923417/may-a-compiler-optimize-a-reference-to-constant-parameter-to-constant-value) but I don't think this kind of optimization would work any other way like constant/non-constant value to reference or even reference to non-constant to constant/non-constant value.
Pooria