Pooria
Pooria

Reputation: 2055

Any tool to determine most efficient way of passing data, by reference or by value?

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 ?

Upvotes: 1

Views: 175

Answers (3)

AshleysBrain
AshleysBrain

Reputation: 22591

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.

Upvotes: 4

Lie Ryan
Lie Ryan

Reputation: 64845

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.

Upvotes: 5

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

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.,

Upvotes: 6

Related Questions