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 ?
views:
93answers:
3Yes, 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.,
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.
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.