Source of program delays

I just got an electron [rev 4.8 ]and am exploring its capability to operate the built in DAC

The code I am using compiled and loaded but has some unusually long delays.|

from Analog write to successive analogWrites is 5 milliseconds. There are no delay calls in the loop. Surely an if statment can’t be using up so much time. I have done a similar program on the slower Teensy processor and it whizzes by

The time LED2 is on is 45usec
Between analogWrites is 5 milliseconds. What is using up the time?

CODE

int dacval =0; 
int led1 = D6; 
int led2 = D7;

void setup() {
  pinMode(DAC1, OUTPUT); 
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

}

void loop() {
    
    digitalWrite(led2, HIGH);
    pinMode(DAC1, OUTPUT);
    analogWrite(DAC1, dacval);
    digitalWrite(led2, LOW);
    if (dacval >4093){ 
    dacval=0;
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    }
    //delay(100);
    dacval++;
}

First, I’d update your Electron to 0.5.3
Then you can drop the extra pinMode(DAC1, OUTPUT); on each iteration of loop() and you need to be aware, that loop() is not an actual loop but only a function that is called from a while(1) int main(), but main() calls several other functions too. and the cloud housekeeping takes at least 1ms per iteration (when connected) - with flaky connection that could become longer.

If you run that code in SYSTEM_MODE(MANUAL) you will see loop() iterating by factors faster.
Another line to throw in your code is SYSTEM_THREAD(ENABLED) which decouples the cloudkepping from your application thread.