# speed of if ()

To evaluate the speed of if() I wrote
the following simple programm:
The essential thing is to run an if statement multiple times in a for loop and measure the time necessary for the whole for loop with the if statements included and not included.
If I comment out the line with the if… the time shown is the same as with the if 's (+/-4µs).
Could that be true? All the math in the brackets should consume some time.
What is the bug?

int a=1;
int b=2;
int c=3;
int d=4;
int result;
long t1;
long i;

void setup() {
Serial.begin(250000);
}

void loop() {
t1=micros();
for(i=0;i<100000;++i)
{
if(a==1&&b==2&&c==3&&d==4){result=abc*d;} // comment out this line for comparison
}
Serial.print(micros()-t1);Serial.println(“µs”);
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
Serial.println(result);
Serial.println(i);
Serial.println();
delay(1000);
}

Since the if statement in that code has no effect the compiler probably optimized it away.

You never change the value of a, b, c, or d. The compiler sees that the if statement will always be true, so it removes the if statement and replaces it with the calculation. It is also likely seeing that you are doing the exact same calculation 100000 times, so eliminates the for statement as well.

As a comparison, if you change the loop() function to the following, you will notice the compiled code size is exactly the same as your original sketch.

``````void loop() {
t1 = micros();
i = 100000;
result = a * b * c * d;
Serial.print(micros() - t1); Serial.println("µs");
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
Serial.println(result);
Serial.println(i);
Serial.println();
delay(1000);
}
``````

One way to get around the compiler optimization is to declare the variables as volatile. This tells the compiler that something external to your code may change its value, and not to assume it will always be the same. As an example:

``````volatile int a = 1;
volatile int b = 2;
volatile int c = 3;
volatile int d = 4;
``````

(note: edited to declare all variables as volatile, so that none get optimized out of the if statement)

## Thanks to all for answering so promptly, especially thank you David, your hint solved it. Volatile does it. I changed the program and tested it on a Nano328. Conclusion: Some µs for an if(). For exact values see below in the comments, if you want.

volatile int a=1;
volatile int b=2;
volatile int c=3;
volatile int d=4;
volatile int result;
volatile long t1;
volatile long i;

void setup() {
Serial.begin(250000);
}

void loop() { // on NANO 328
t1=micros();
for(i=0;i<100000;++i)
{
// if(a==1&&b==2&&c==3&&d==4){result=abcd;} // 735704µs @ compare allmatch + math
// if(a==9&&b==2&&c==3&&d==4){result=a
bcd;} // 270388µs @ compare mismatch first + math
// if(a==1&&b==2&&c==3&&d==9){result=abc*d;} // 402436µs @ compare mismatch last + math
// if(a==1&&b==2&&c==3&&d==4){ } // 377284µs @ compare allmatch no math
// if(a==9&&b==2&&c==3&&d==4){ } // 270388µs @ compare mismatch first no math
// if(a==1&&b==2&&c==3&&d==9){ } // 377284µs @ compare mismatch last no math

}
Serial.print(micros()-t1);Serial.println(“µs”);
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
Serial.println(result);
Serial.println(i);
Serial.println();
delay(1000);
}