Benchmarks using R3 and R4 Minima

In many different cases you need a square-wave signal of a certain frequency. The most easy (but not efficient) way is to set an Arduino pin alternatively to HIGH and LOW. This can be done by some code like this. Note that there are two ways to toggle the pin (version 1 and version 2).

const byte pin = 2;
boolean val = false;

void setup() {
  /*
    Serial.begin(9600);
    while (!Serial);
    Serial.println(__FILE__ " " __TIME__);
    Serial.end();
    /* */
  pinMode(pin, OUTPUT);
  while (true) {
    // version 1:
    //digitalWrite(pin, val = !val);
    // version 2:
    digitalWrite(pin, HIGH);
    digitalWrite(pin, LOW);
  }
}

void loop() {}

(The while-loop might have been replaced by the standard void-loop function but this loop also does some extra house-keeping.)
The Serial-code actually is deactivated but can be activated by adding a slash just before /*.

These are the results comparing an R3 to an R4 (Minima):
R3:
version 1: 166.015 Hz
version 2: 159.355 Hz
(no matter if Serial had been activated or not)
R4:
version 1:
without Serial: 543.910 Hz
with Serial: 498.625 Hz
version 2:
without Serial: 544.000 Hz
with Serial: 583.775 Hz

As R4 is operated with 48 MHz compared to R3 operated with 16 MHz it is clear that its perfomance is about three times higher, even a little bit more. I did not believe my eyes when I saw the results; it is completely unclear to me: version 1 is getting slower when Serial is temporarily activated but version 2 is getting faster when Serial is temporarily activated.
You are kindly invited to do your own tests.

I don't see any reason why "activating" Serial should make the loop run slower, since you're not actually USING it at the time; that's weird.

you are right, actually it is more than weird. That is why I did another test, using the R4 WiFi. Even stanger. The results:
version 1:
without Serial:
497.230 - 497.300 Hz
with Serial:
542.280 - 542.330 Hz
version 2:
without Serial:
568.100 - 568.200 Hz
with Serial:
611.777 - 611.890 Hz
(not sure if is is an original R4 or a clone, they look very similar)
So it seems Serial.end() does not clean-up completely.
If it was ATMEL I would trace the ASM, but not with Renesas.

Why not? You have objdump also for Renesas ... you can find it in the IDE by following the path of Renesas package :

arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/bin/objdump

... same syntax as for AVR :wink:

Guglielmo

you are kindly invited to do so.
R3: 1920 bytes in well-known environment
R4: 52352 byte in completely unknown environment.
No, I am definitly not going to trace it.