I think about conditionals and compilers. I am programming an application for Arduino and so I need the application to be as fast as possible.
In my code I have this:
#define DEBUG false
...
if (DEBUG)
{
String pinName;
pinName = "Pin ";
pinName += pin;
pinName += " initialized";
Serial.println(pinName);
}
I am wondering if the compiler does not include the code (code in if block) in binary file. The conditions is always false, so the program never goes there.
And from the other side. What if DEBUG is true? Does Arduino test the condition or the compiler include only the body of if in binary file?
I found this site https://gcc.gnu.org/onlinedocs/gcc-3.0.2/cpp_4.html about #if directive, so I can rewrite the code to have these directives instead of "normal" if. But I would like to know if I should rewrite it or if it would be waste of time.
DEBUG
isfalse
. – Sinesif (CONSTANT) { ... }
is very easy for a compiler to optimize. I would expect that any decent compiler from at least the last 25 years should be able to remove the conditional branch. – Pesek#if
/#endif
, even if it doesn't make a difference in the binary. That way it's clear whatDEBUG
is. – Albigenses