// 007: how many times per second the real Arduino loop() runs, without and with one println().
// BAUD passed with -D. Phase 1 (5 s): empty loop. Phase 2 (5 s): loop with println. Then report.
#ifndef BAUD
#define BAUD 115200
#endif
#include "esp_timer.h"

volatile float temp = 23.45f;
uint32_t n = 0;
uint32_t perSec[2][5];
int phase = 0, sec = 0;
int64_t tNext;

void setup() {
  Serial.begin(BAUD);
  delay(300);
  Serial.printf("# loops: baud=%d\n", BAUD);
  Serial.flush();
  delay(100);
  tNext = esp_timer_get_time() + 1000000;
}

void loop() {
  n++;
  if (phase == 1) { Serial.print("temp: "); Serial.println(temp); }
  if (phase < 2 && esp_timer_get_time() >= tNext) {
    perSec[phase][sec] = n; n = 0; tNext += 1000000;
    if (++sec == 5) {
      sec = 0; phase++;
      if (phase == 1) { tNext = esp_timer_get_time() + 1000000; n = 0; }
    }
    if (phase == 2) {
      Serial.flush(); delay(200);
      Serial.print("\n");
      for (int p = 0; p < 2; p++) {
        Serial.printf("RESULT %s", p ? "print_loops_per_s=" : "empty_loops_per_s=");
        for (int i = 0; i < 5; i++) Serial.printf("%lu%s", (unsigned long)perSec[p][i], i < 4 ? ";" : "\n");
      }
      Serial.println("DONE");
    }
  }
}
