Switch off compiler optimization

I made a test of computation time on a Due. The result shows an extremely fast processor which is impossible:

#define SERPRT(s, v, r) \
  Serial.print(F(s)); \
  if (r) Serial.println(v); \
  else Serial.print(v);

void setup() {
  Serial.begin(38400);
  delay(10);
  Serial.println();
  Serial.println("----------------------------");

#define NSKALENT 6

float Tskal=0.00001, fT=1000000;
uint32_t Sample_tot = 150000;
uint32_t Dat0vis;
float Sample_pix;
uint16_t BarWidth=309, PlotSize=308;
float Tvis, T0vis=0.05;
uint16_t movSmall=3;
uint16_t SlPos, SliderLen;
uint32_t t0=micros(), tm, hh=0;

delay(1);
tm = micros();
Serial.println(tm-t0);

t0=micros();
for (int i=0; i<10000; i++) {
  Sample_pix = Tskal * fT * NSKALENT / PlotSize;
  Tvis = Tskal * NSKALENT;
  T0vis = Tskal * NSKALENT / movSmall;
  Dat0vis = T0vis * fT;
  SlPos = 6 + BarWidth * T0vis * fT /  Sample_tot;
  SliderLen = Tskal * NSKALENT * BarWidth * fT / Sample_tot;
  hh++;
}
tm=micros();
SERPRT("runs: ", hh, 1);
Serial.print("Zeit: ");
Serial.println( tm-t0);
SERPRT("Sample_pix=", Sample_pix, 0);
SERPRT(" Tvis=", Tvis, 0);
SERPRT(" T0vis=", T0vis, 1);
SERPRT("Dat0vis=", Dat0vis, 0);
SERPRT(" SlPos=", SlPos, 0);
SERPRT(" SliderLen=", SliderLen, 1);

}

Serial Monitor shows 1 us processing time:

----------------------------
842
runs: 10000
Zeit: 1
Sample_pix=0.19 Tvis=0.00 T0vis=0.00
Dat0vis=20 SlPos=6 SliderLen=0

Even if the compiler is optimizing away a lot of code, 1 us processing time for 10000 times incrementing hh is impossible for an 84 MHz controller.

Is it possible that compiler optimization causes such a short processing time?

And the micros() timing is bad. A 1 ms delay is measured as 842 us.

It appears that most of the data is constants. The compiler could eliminate the loop and just compute the final result. Adding volatile to some of them may force it to actually do the loop.

You could also add

#pragma GCC optimize ("O0")

at the start of your code to disable optimization.

It surely is. The compiler can rather easily calculate the results of executing that loop, and skip the "looping".

None of these calculations depend on "i" so why bother calculating them inside the loop?

  Sample_pix = Tskal * fT * NSKALENT / PlotSize;
  Tvis = Tskal * NSKALENT;
  T0vis = Tskal * NSKALENT / movSmall;
  Dat0vis = T0vis * fT;
  SlPos = 6 + BarWidth * T0vis * fT /  Sample_tot;
  SliderLen = Tskal * NSKALENT * BarWidth * fT / Sample_tot;