Arduino Due analogWrite duration changes when unrelated code is changed

Hello,

I am trying to get my Arduino Due to produce sound waves at very accurate frequencies. In order to do this I measured how long on analogWrite iteration takes and calculated how many iterations are required for a specific frequency etc.

The problem can not figure out is why the time it takes to perform one analogWrite changes when I change seemingly unrelated code. This code that I change is only executed after all of the time measurements are finished.

int loopCount = 10000;
void loop() {    
    long time1 = micros();
    for(int p=0; p<loopCount; p+=1){
      for (int i = 0; i < sampleSize; i+=1) {
        analogWrite(DAC1, sineValues[i]);
      }
    }
    long time2 = micros();
    int deltaTime = (time2 - time1)/loopCount;
    Serial.println(deltaTime);
    double frequency = 1000000/deltaTime;
    Serial.print("Frequency: ");
    Serial.print(frequency);
    Serial.println(" Hz");
    Serial.println((float)deltaTime/sampleSize);
}

When executing the above code the value of deltaTime is 5046 us indicating one analogWrite takes 5046 us.

int loopCount = 10000;
void loop() {    
    long time1 = micros();
    for(int p=0; p<loopCount; p+=1){
      for (int i = 0; i < sampleSize; i+=1) {
        analogWrite(DAC1, sineValues[i]);
      }
    }
    long time2 = micros();
    int deltaTime = (time2 - time1)/loopCount;
    Serial.println(deltaTime);
    /*double frequency = 1000000/deltaTime;
    Serial.print("Frequency: ");
    Serial.print(frequency);
    Serial.println(" Hz");
    Serial.println((float)deltaTime/sampleSize);*/
}

When I comment out some of the code in the main loop the value of deltaTime is different, namely 5281 us.

Note that these values are very constant, when I uncomment the code deltaTime becomes exactly 5046 us again.

I hope anyone has an idea of what causes this.
Thanks

oops.
Just noticed that you were using DUE.
The issues I noticed were for AVR, so while the issue might be similar and related to optimizations, you can ignore the rest of my post...
--- bill


What IDE version?
The newer avr gcc compiler in IDE around 1.6.10 or 11 does some pretty crazy code folding optimizations.
I haven't figured out what triggers it but it can insert squash things down and even insert external routines to inline them. Just changing a single line of code can trigger completely different code generation.
The final code is sometimes pretty unrecognisable.

The biggest changes I've seen were saving call overheads and register saves/restores because of avoiding function pre/post preambles.

I have seen calls to digitalWrite() swing from about 4.5us 7.1us whereas it used to be a pretty consistent time close to about 6us.
And lots of other crazy differences.

The best thing to do (and pretty much the only way to know) will be to look at the assembler output to see what code the compiler actually generated.

--- bill

http://forum.arduino.cc/index.php?topic=224672.0