I've often read that global variables have a slight performance edge over local variables, so I decided to test it out.
The code below compares the times t1 and t2 for two for loops:
t1: the loop index j being assigned to a global variable 1000 times
t2: the loop index k being assigned to a local variable declared and initialized within the for loop 1000 times
float t1,t2;
float var1;
void setup() {
Serial.begin(115200);
delay(1500);
}
void loop() {
measure();
delay(2000);
}
void measure(){
t1 = micros();
for (int j = 0; j<1000; j++){
var1 = float(j);
}
t1 = micros() - t1;
t2 = micros();
for (int k = 0; k<1000; k++){
float var2 = float(k);
}
t2 = micros() - t2;
Serial.print("t1: ");
Serial.print(t1);
Serial.print("us, t2: ");
Serial.print(t2);
Serial.print("us, Difference: ");
Serial.println(t2-t1);
}
There are three cases, all run on a 16MHz Pro Micro:
- When variables are integers, there is little/no difference between t1 and t2. Seems to be hitting the resolution of the micros() function.
t1: 4us, t2: 4us, Difference: 0
t1: 4us, t2: 4us, Difference: 0
t1: 4us, t2: 0us, Difference: -4
t1: 4us, t2: 4us, Difference: 0
t1: 4us, t2: 4us, Difference: 0
t1: 4us, t2: 4us, Difference: 0
t1: 4us, t2: 4us, Difference: 0
t1: 4us, t2: 0us, Difference: -4
t1: 4us, t2: 0us, Difference: -4
t1: 4us, t2: 4us, Difference: 0
t1: 4us, t2: 4us, Difference: 0
t1: 0us, t2: 4us, Difference: 4
t1: 4us, t2: 4us, Difference: 0
t1: 0us, t2: 4us, Difference: 4
- When all the variables (including the for loop indices i and k) are changed to float, t1 increases to 7000+ us while t2 does increase up to 12us occasionally.
t1: 7452.00us, t2: 4.00us, Difference: -7448.00
t1: 7452.00us, t2: 4.00us, Difference: -7448.00
t1: 7452.00us, t2: 8.00us, Difference: -7444.00
t1: 7460.00us, t2: 4.00us, Difference: -7456.00
t1: 7460.00us, t2: 4.00us, Difference: -7456.00
t1: 7468.00us, t2: 4.00us, Difference: -7464.00
t1: 7460.00us, t2: 4.00us, Difference: -7456.00
t1: 7456.00us, t2: 4.00us, Difference: -7452.00
t1: 7452.00us, t2: 12.00us, Difference: -7440.00
t1: 7452.00us, t2: 4.00us, Difference: -7448.00
t1: 7448.00us, t2: 4.00us, Difference: -7444.00
t1: 7460.00us, t2: 8.00us, Difference: -7452.00
- When the variables are changed to float, but the for loop indices are still integers (the code shown above). They are typecast to float when being assigned to the variables. This shows improved performance in global variables, and slightly slower for local variables.
t1: 4536.00us, t2: 0.00us, Difference: -4536.00
t1: 4528.00us, t2: 8.00us, Difference: -4520.00
t1: 4528.00us, t2: 8.00us, Difference: -4520.00
t1: 4544.00us, t2: 8.00us, Difference: -4536.00
t1: 4536.00us, t2: 8.00us, Difference: -4528.00
t1: 4528.00us, t2: 8.00us, Difference: -4520.00
t1: 4536.00us, t2: 0.00us, Difference: -4536.00
t1: 4536.00us, t2: 0.00us, Difference: -4536.00
t1: 4528.00us, t2: 8.00us, Difference: -4520.00
t1: 4528.00us, t2: 8.00us, Difference: -4520.00
t1: 4528.00us, t2: 16.00us, Difference: -4512.00
t1: 4528.00us, t2: 16.00us, Difference: -4512.00
t1: 4536.00us, t2: 8.00us, Difference: -4528.00
t1: 4528.00us, t2: 8.00us, Difference: -4520.00
Can anyone shed any light on why local variables seem to be created, assigned a value, and destroyed faster than a global variable is assigned a value?
Is it a C++ thing or an Arduino/AtMega issue?
Is my testing methodology wrong?