user66858: Consider this code:
void loop() {
    digitalWrite(pinTest, HIGH);
    digitalWrite(pinTest, LOW);
}

On an Arduino Mega 2560, running 16MHz (=0.06us), I would expect the width of the pulse to be somewhere around 0.1us.
However, when measuring with my oscilloscope, I get around 4us high and 5-6 us low. I understand it could take some cycles to run the digitalWrite code, but this seems quite a lot.
How is that difference explained?

user7508: You do not need to go all the way down to assembly in order to get that
speed. You can do direct port access from your .ino file:
void setup() {
    DDRA |= _BV(PA0);  // pin 22 = PA0 as output
    for (;;) {
        PINA |= _BV(PA0);  // toggle PA0
    }
}

void loop(){}

This compiles to something that is almost equivalent to your assembly
code. Actually, it is a bit faster, as it uses rjmp instead of the
slower jmp instruction.
Edit: A few notes

As pointed out by timemage in a comment, you can save another CPU
cycle by writing PINA = instead of PINA |=.

This code, as well as your two examples, will exhibit a glitch every
1,024 µs. This is caused by the periodic timer interrupt used
by the Arduino core for timekeeping (millis(), micros() and
delay()). You can avoid the glitch by disabling interrupts before
going into the tight loop. Alternatively, if you do not use the
Arduino core at all, you can define a function called main()
instead of setup() and loop(): this will completely remove the
Arduino core initialization for your compiled program.

arduino-cli is useful for sparing you the complexity of the Arduino
build system (automatic installation of the cores and libraries,
libraries in multiple places that depend on the core you use...). If
you do not use the Arduino core, arduino-cli is of little use: a
very simple Makefile that calls avr-gcc and avrdude is all you
need for basic AVR development.



66858: <p>Consider this code:</p>
<pre><code>void loop() {
    digitalWrite(pinTest, HIGH);
    digitalWrite(pinTest, LOW);
}
</code></pre>
<p>On an Arduino Mega 2560, running 16MHz (=0.06us), I would expect the width of the pulse to be somewhere around 0.1us.</p>
<p>However, when measuring with my oscilloscope, I get around 4us high and 5-6 us low. I understand it could take some cycles to run the <code>digitalWrite</code> code, but this seems quite a lot.</p>
<p>How is that difference explained?</p>
 7508: <p>You do not need to go all the way down to assembly in order to get that
speed. You can do direct port access from your <code>.ino</code> file:</p>
<pre class="lang-cpp prettyprint-override"><code>void setup() {
    DDRA |= _BV(PA0);  // pin 22 = PA0 as output
    for (;;) {
        PINA |= _BV(PA0);  // toggle PA0
    }
}

void loop(){}
</code></pre>
<p>This compiles to something that is almost equivalent to your assembly
code. Actually, it is a bit faster, as it uses <code>rjmp</code> instead of the
slower <code>jmp</code> instruction.</p>
<p><strong>Edit</strong>: A few notes</p>
<ol>
<li><p>As pointed out by timemage in a comment, you can save another CPU
cycle by writing <code>PINA =</code> instead of <code>PINA |=</code>.</p>
</li>
<li><p>This code, as well as your two examples, will exhibit a glitch every
1,024 µs. This is caused by the periodic timer interrupt used
by the Arduino core for timekeeping (<code>millis()</code>, <code>micros()</code> and
<code>delay()</code>). You can avoid the glitch by disabling interrupts before
going into the tight loop. Alternatively, if you do not use the
Arduino core at all, you can define a function called <code>main()</code>
instead of <code>setup()</code> and <code>loop()</code>: this will completely remove the
Arduino core initialization for your compiled program.</p>
</li>
<li><p><code>arduino-cli</code> is useful for sparing you the complexity of the Arduino
build system (automatic installation of the cores and libraries,
libraries in multiple places that depend on the core you use...). If
you do not use the Arduino core, <code>arduino-cli</code> is of little use: a
very simple Makefile that calls <code>avr-gcc</code> and <code>avrdude</code> is all you
need for basic AVR development.</p>
</li>
</ol>
