If the TEST macro is not defined, I would like to know whether there is a performance difference in these two pieces of code:
void Func1(int a) {
// ...
}
#ifdef TEST
Func1(123);
#endif
and:
void Func2(int a) {
#ifdef TEST
// ...
#endif
}
Func2(123);
With TEST not defined, Func2 would become an empty function that the compiler should not call at all, isn’t it?
It pretty much comes down to whether that particular call to
Func2is inlined or not. If it is, then an optimizing compiler ought to be able to make an inlined call to an empty function the same as not calling it at all. If it isn’t inlined, then it’s called and returns immediately.As long as the function definition is available in the TU containing the call to
Func2, there’s no obvious reason it won’t be inlined.This all relies on the fact that
123is a literal, so evaluating the arguments of your call has no side-effects. The args have to be evaluated even if the function call has no effect, so: