If I write a function like this:
void doMaybeNothing()
{
#ifndef IM_LAZY
doSomething();
#endif
}
Are modern compilers smart enough to detect a no-op function and optimize so that there are no cycles wasted? Or is there always a small performance impact?
Assuming the body of the function is available at compile-time or link-time (i.e., it's not in a dynamically linked library), most modern compilers should get rid of calls to functions that do nothing (if optimizations are enabled, of course).
Effectively, this is just a form of inline expansion, which allows the body of a function to be expanded anywhere it is called, so long as the results are the same. If the function does nothing, then it will simply expand to nothing wherever it is inlined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With