I compiled the following two sketches with the compiler used by the Arduino IDE and ran them on an Arduino UNO (smd edition). I'm using:
- Arduino IDE v. 1.8.0
- Arduino AVR Boards v. 1.6.20
1.
void setup() {
Serial.begin(115200);
unsigned long a, b;
//unsigned long c, d;
a = micros();
for (unsigned long i = 0; i != 1000000; i++) digitalRead(13);
b = micros();
Serial.println(b - a);
//c = micros();
//for (unsigned long i = 0; i != 1000000; i++) digitalRead(13);
//d = micros();
//Serial.println(d - c);
}
void loop () {}
2.
void setup() {
Serial.begin(115200);
unsigned long a, b;
unsigned long c, d;
a = micros();
for (unsigned long i = 0; i != 1000000; i++) digitalRead(13);
b = micros();
Serial.println(b - a);
c = micros();
for (unsigned long i = 0; i != 1000000; i++) digitalRead(13);
d = micros();
Serial.println(d - c);
}
void loop () {}
These were the outputs on the serial monitor:
2452320
3332640 3332704
Why did each of the two for
loops in the second sketch take roughly 33% more time to execute than the loop in the first one?