What happens outside of loop()?

I observed this with a Arudiono Pro Mini atamega328p running @ 16Mhz and an oscilloscope. If I run something like this

void loop() {
  led on
  ...
  led off
}

The interval from led off to led on is about 4 usec. However if I run this

void loop() {
  for (;;) {
    led on
    ...
    led off
  }
}

The interval is less than 1 usec.

What happens outside of loop()? Does the call/return to loop really cost 3*16=48 cycles?

Have a look at the code! Its open source so you have it all, and the top-level code
is in main.cpp:

int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

So the loop that calls loop() also checks for serial events. Note that what's not
shown here is the hidden initialization of all static C++ instances which have
their constructors called before main runs (and before any of the hardware
is initialized - thus you may not call any UI functions in constructors, just
initialize instance variables.)

Why not create your own sub-function that sets and calls the led and run it inside your for() loop? That should answer any questions. There might also be some variable initialization as part of starting loop() depending on how many local variables you create and initialize.

Since your code is pseudo-code it is hard to say exactly.

Thanks MarkT. This explains it. In my case I have my own serial so doing my own loop should be fine. Possibly additional speed improvement is due to better variable caching across loop iterations.

Where can I find the source code?