is this due to compiler-optimisation?

Hi everybody,

i was just writing a hsort demo-code that should illustrate how fast a loop can run
So I wrote this version

unsigned long myCount = 1;
unsigned long StartTime;
unsigned long StopTime;
unsigned long Cnt_target = 100000000;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
}

void loop() {
  myCount = 0;
  StartTime = micros();
  while (myCount < Cnt_target) {
    myCount++;
  }
  
  StopTime = micros();
  Serial.print("StartTime");
  Serial.println (StartTime);

  Serial.print("StopTime");
  Serial.println (StopTime);
  
  Serial.print("counting up from 0 to ");
  Serial.print (Cnt_target);
  Serial.print( " needed ");
  Serial.print(StopTime - StartTime);
  Serial.println(" microseconds");
   
  Serial.print("This means a single loop needs ");
  Serial.print( (StopTime - StartTime) * 1000 / Cnt_target);
  Serial.println(" nanoseconds");    
}

which produces this output

Setup-Start
17:20:13.601 -> StartTime116
17:20:13.601 -> StopTime120
17:20:13.601 -> counting up from 0 to 100000000 needed 4 microseconds
17:20:13.601 -> This means a single loop needs 0 nanoseconds
17:20:13.601 -> StartTime6492
17:20:13.601 -> StopTime6496
17:20:13.601 -> counting up from 0 to 100000000 needed 4 microseconds
17:20:13.601 -> This means a single loop needs 0 nanoseconds
17:20:13.601 -> StartTime17544
17:20:13.601 -> StopTime17544
17:20:13.601 -> counting up from 0 to 100000000 needed 0 microseconds
17:20:13.601 -> This means a single loop needs 0 nanoseconds

Huh ?! What? counting up to 10.0000.000 in just 4 µseconds?
Can I develop the next Apple-laptop? (just kidding)
after scratching my head about this result I remembered there was something about code-optimasation
The counting up is "standalone" and this might make the compiler throw it away
ANd I wrote a version that uses variable myCount before and after the while loop.
This behaves as expected.

17:23:59.527 -> Setup-Start
17:23:59.527 -> counting up myCount starts at 0
17:24:00.375 -> counting up myCount stopped at 1000000
17:24:00.375 -> StartTime116
17:24:00.375 -> StopTime818696
17:24:00.375 -> counting up from 0 to 1000000 needed 818580 microseconds
17:24:00.375 -> This means a single loop needs 818 nanoseconds
17:24:00.375 -> 
17:24:00.375 -> 
17:24:00.375 -> counting up myCount starts at 0
17:24:01.179 -> counting up myCount stopped at 1000000
17:24:01.179 -> StartTime827892
17:24:01.179 -> StopTime1649144
17:24:01.179 -> counting up from 0 to 1000000 needed 821252 microseconds
17:24:01.226 -> This means a single loop needs 821 nanoseconds

so is this due to code-optimisation?
best regards Stefan

4 microseconds, nice.... :grin:

I changed

 unsigned long myCount = 1;

to

 volatile unsigned long myCount = 1;

, and the "speed" is gone...

Erik_Baas:
4 microseconds, nice.... :grin:

I changed

 unsigned long myCount = 1;

to

 volatile unsigned long myCount = 1;

, and the "speed" is gone...

Volatile will slow the test, because the variable has to be read and written to memory every time it is used. I would add a random number to Cnt_target, that prevents optimizing the code even though the random number is always the same when using random() .