Compiler efficiency ! ? ! ?

I am a novice at this and wanted to see just how quickly (or efficiently) the compiler is wrt to, say, a simple pin write function. So I have two super simple sketches (SSS), and I monitor the pin toggle speed with an oscilloscope:

void setup() {
  pinMode(13, OUTPUT); 
}
void loop() {
  digitalWrite(13,HIGH);
  digitalWrite(13,LOW); 
  digitalWrite(13,HIGH);
  digitalWrite(13,LOW); 
}

******** Cycle time = 7.7us.

Now use some simple instructions:

void loop() {
  PORTB |= B00100000;
  PORTB &= B11011111;
  PORTB |=  B00100000;
  PORTB &= B11011111;
}

******** Cycle time = 250ns . . . . that is more than 30X faster . . . 2 cycles each to be exact! Just what you expected. This is not a good indictment if you are looking for any kind of speed. Why is it so bad?

Why is it so bad?

The first code is portable, and it's timing has very little to do with compiler efficiency.
The second is not portable

You have the source - why not take a look?

Look at the code behind digitalWrite, all the checking it does.
Not a compiler issue.

Try this too:

void loop(){
PIND = 0b00100000; // toggle output by writing 1 to input register
}
or
void loop(){
while(1){                   // stay away from (unseen main) calling loop function code
PIND = 0b00100000; // toggle output by writing 1 to input register
}
}

That was way quick guys . . . thanks. I don't know how to examine the code ! :disappointed_relieved: :disappointed_relieved:

hardware/arduino/cores/arduino/wiring_digital.c