Global vs Local variable performance

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:

  1. 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
  1. 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
  1. 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?

Is my testing methodology wrong?

Don't forget that the compiler may well be optimising your code

potatochip:
I've often read that global variables have a slight performance edge over local variables, so I decided to test it out.

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?

Any speed differences you may see will be due to the way the compiler generates and optimizes the code. The AVR (Arduino) is just a CPU... it doesn't know or care whether your variables are local or global. SRAM is SRAM.

Are you just curious or do you actually care about a few microseconds plus or minus?

Rather than worry about speed, it would probably be better for you to learn the difference between local and global variables and the advantages / disadvantages of each. For that matter, look into "volatile" and "static" types as well.

Each has their place.

The compiler has no trouble figuring out that the result of the following loop is j=1000 and var1=999.0. So, it may dispense with the entire loop.

 for (int j = 0; j<1000; j++){
    var1 = float(j);
  }

Krupski:
Any speed differences you may see will be due to the way the compiler generates and optimizes the code. The AVR (Arduino) is just a CPU... it doesn't know or care whether your variables are local or global. SRAM is SRAM.

Are you just curious or do you actually care about a few microseconds plus or minus?

Rather than worry about speed, it would probably be better for you to learn the difference between local and global variables and the advantages / disadvantages of each. For that matter, look into "volatile" and "static" types as well.

Each has their place.

Speed is important to me. I need to collect realtime-ish data, so the faster the loop, the more datapoints I can get. This test is just a thousand iterations, but I imagine when the program runs for several minutes, the difference adds up.

I was using global variables until now because I was led to believe they worked slightly faster, but that does not always seem to the case with floating point variables.

Suppose there is a bit of curiosity as well, or I wouldn't be asking here.

potatochip:
Speed is important to me. I need to collect realtime-ish data, so the faster the loop, the more datapoints I can get. This test is just a thousand iterations, but I imagine when the program runs for several minutes, the difference adds up.

I was using global variables until now because I was led to believe they worked slightly faster, but that does not always seem to the case with floating point variables.

Suppose there is a bit of curiosity as well, or I wouldn't be asking here.

I'm not trying to be a smarta$$... I just wondered why you cared so that I could give you pertinent information.

I think you will find that with compiler optimizations, it will be almost impossible to predict whether local or global will be faster, and in any case the difference is so small that it's really irrelevant.

If you need more speed, stick a faster crystal on your board. I run all of mine at 22.1184 MHz. and they are rock solid.

I've tested them up to 30 MHz. and they are stable, so 22.1184 is OK (and it gives you perfect 0% error baud rates too!)

jremington:
The compiler has no trouble figuring out that the result of the following loop is j=1000 and var1=999.0. So, it may dispense with the entire loop.

 for (int j = 0; j<1000; j++){

var1 = float(j);
 }

Excellent point!

I changed the maximum value and t1 does change accordingly.

But what you said does apply for the second loop (k) with the local variable. Changing the number of iterations had no effect.

So I decided to do something with the variables in both loops:

 t1 = micros();
  for (int j = 0; j<1000; j++){
    var1 = float(j);
    analogWrite(A1,var1/4);
  }
  t1 = micros() - t1;

  t2 = micros();
  for (int k = 0; k<1000; k++){
    float var2 = float(k);
    analogWrite(A1,var2/4);
  }
  t2 = micros() - t2;

Output:

t1: 305528.00us, t2: 300432.00us, Difference: -5096.00
t1: 305508.00us, t2: 300440.00us, Difference: -5068.00
t1: 305508.00us, t2: 300444.00us, Difference: -5064.00
t1: 305512.00us, t2: 300440.00us, Difference: -5072.00
t1: 305508.00us, t2: 300448.00us, Difference: -5060.00
t1: 305508.00us, t2: 300440.00us, Difference: -5068.00

The local loop still has a small but consistent (when changing the number of iterations) 1.7% lead over the global loop. And obviously that percentage will change depending on what else is done in the loop.

Also, using integer variables:

t1: 13484us, t2: 13224us, Difference: -260
t1: 13484us, t2: 13228us, Difference: -256
t1: 13476us, t2: 13224us, Difference: -252
t1: 13484us, t2: 13224us, Difference: -260
t1: 13476us, t2: 13232us, Difference: -244
t1: 13484us, t2: 13224us, Difference: -260
t1: 13476us, t2: 13236us, Difference: -240

So, there is still a difference, but it's not nearly as large as I first thought.

Didn't expect local to be faster though.

One of the reasons that a global might be faster than an auto is that the global can use absolute addressing and the auto addressing relative to the stack pointer.

Krupski:
I'm not trying to be a smarta$$... I just wondered why you cared so that I could give you pertinent information.

I think you will find that with compiler optimizations, it will be almost impossible to predict whether local or global will be faster, and in any case the difference is so small that it's really irrelevant.

If you need more speed, stick a faster crystal on your board. I run all of mine at 22.1184 MHz. and they are rock solid.

I've tested them up to 30 MHz. and they are stable, so 22.1184 is OK (and it gives you perfect 0% error baud rates too!)

You were right about it being irrelevant (see previous post). Thanks for your input!