Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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!

like image 742
itsleruckus Avatar asked Dec 19 '22 05:12

itsleruckus


1 Answers

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.

like image 61
Columbo Avatar answered Dec 21 '22 18:12

Columbo