// Single precision speed test using Pi calculation.
// by Koepel, Public Domain
// 22 jan 2022, Version 1
//
//
// Select the board in diagram.json with:
//   "wokwi-arduino-uno"
//   "wokwi-arduino-mega"
//   "wokwi-pi-pico" with attribute "env" set to "arduino-core" and "arduino-community"
//   "wokwi-esp32"
//
// The Pico needs connections:
//   "connections": 
//   [ 
//     [ "$serialMonitor:RX", "pico:GP0", "", [] ], 
//     [ "$serialMonitor:TX", "pico:GP1", "", [] ] 
//   ]



#if defined(ARDUINO_ARCH_RP2040)
  #if defined(ARDUINO_ARCH_MBED)
    #define _SERIAL Serial1
    char extraMessage[] = "Raspberry Pi Pico + Mbed, \"arduino-core\"";
  #else
    #define _SERIAL Serial1
    char extraMessage[] = "Raspberry Pi Pico, \"arduino-community\"";
  #endif
#elif defined(ARDUINO_ARCH_ESP32)
  #define _SERIAL Serial
  char extraMessage[] = "ESP32";
#elif defined(ARDUINO_ARCH_AVR)
  #if defined(__AVR_ATmega328P__)
    #define _SERIAL Serial
    char extraMessage[] = "Arduino Uno";
  #elif defined(__AVR_ATmega2560__)
    #define _SERIAL Serial
    char extraMessage[] = "Arduino Mega 2560";
  #else
    #define _SERIAL Serial
    char extraMessage[] = "Unknown AVR board";
  #endif
#else
  #define _SERIAL Serial
  char extraMessage[] = "Unknown board";
#endif


const long n = 100000L;     // start with 1000L, maybe 100000L on a fast computer

void setup()
{
  _SERIAL.begin( 115200);
  _SERIAL.print( "Single precision speedtest (");
  _SERIAL.print( extraMessage);
  _SERIAL.println( ").");
  _SERIAL.print( "Iterations    : ");
  _SERIAL.println( n);

  unsigned long timeStamp1 = millis();

  // Francois Viete formula
  float s = 0.0;
  float t = 1.0;
  for( long i=0; i<n; i++)
  {
    float r = s + 2.0;
    s = sqrt(r);
    t *= s / 2.0;
  }

  unsigned long timeStamp2 = millis();

  unsigned long elapsedMillis = timeStamp2 - timeStamp1;
  _SERIAL.print( "Time          : ");
  _SERIAL.print( float(elapsedMillis) / 1000.0);
  _SERIAL.println( " seconds (the time that it would take in the real world)");
  _SERIAL.print( "M_PI constant : ");
  _SERIAL.println( M_PI, 20);
  _SERIAL.print( "Calculated Pi : ");
  _SERIAL.println( 2.0 / t, 20);
}

void loop() 
{
  delay(1000);
}