Assuming the compiler does in fact inline foo is there a performance difference between these 2 statements?
inline int foo (int val) {
return val;
}
int main () {
std::cout << foo(123) << std::endl;
std::cout << 123 << std::endl;
return 0;
}
Let’s ignore any implications that move semantics and copy elision might have.
My compiler (gcc 4.7.2) produces nearly identical code for the two statements:
The only difference is the order of the first two instructions. I’ve experimented with it, and this difference doesn’t appear to have anything to do with
foo(): if I repeat the two lines twice, only the last of the four statements has the instruction order reversed. This makes me think that this artifact probably has something to do with the pipeline optimizer or something of that nature.