itsleruckus
itsleruckus

Reputation: 43

What is more optimal, store parameter and pass to function or pass to function with call to parameter

New to C++ and trying to learn optimization techniques, so hopefully someone can clarify for me.

Is there a real difference between these two options:

1) Store parameters and pass to function

const char *text = getText(var)
doSomething(text);

2) Pass to function calls for parameters

doSomething(getText(var));

I am not trained enough in computer science to realize the difference when it gets to the compiler stage, unfortunately, so any help would be great!

Upvotes: 4

Views: 67

Answers (2)

Columbo
Columbo

Reputation: 60989

There is a technical difference: In

doSomething(getText(var));

the argument to doSomething is an rvalue, while in

doSomething(text);

The argument is an lvalue. However, in the vast majority of all cases this is irrelevant, and both lines should result in equivalent machine code on any decent compiler, so choose whatever you find to be more readable.

Upvotes: 7

Barry
Barry

Reputation: 303097

Assuming text is only used to call to doSomething(), the compiler will certainly produce identical code in both cases.

The only difference really is how you perceive the readability between the two and how easy it is in a debugger to stop between getText() and doSomething(), in case that's necessary.

Upvotes: 4

Related Questions