Program nejdřív provede výpočet a potom výpis s dobou výpočtu v ms a us. Můžete ho zkusit třeba jako benchmark více desek, mělo by to běžet i na ESP. Jinak benchmark MCU jsem dělal taky a lepší, ten vyjde 19.9.
Tady je kód:
Kód: Vybrat vše
/*
Výpočet čísla pí na 40 desetinných míst
a změření doby samotného výpočtu.
Vhodné pro Arduino UNO, Nano, Mega, ESP32 a další.
*/
constexpr uint8_t DECIMAL_PLACES = 40;
// Jedna číslice před desetinnou čárkou + desetinná místa
constexpr uint16_t PI_DIGITS = DECIMAL_PLACES + 1;
// Velikost pracovního pole pro spigot algoritmus
constexpr uint16_t WORK_SIZE = PI_DIGITS * 10 / 3 + 1;
uint16_t workArray[WORK_SIZE];
// Výsledek obsahuje pomocnou úvodní nulu,
// číslice pí a ukončovací znak
char piRaw[PI_DIGITS + 2];
void appendDigit(uint8_t digit, uint16_t &position)
{
piRaw[position++] = '0' + digit;
}
void calculatePi()
{
for (uint16_t i = 0; i < WORK_SIZE; i++) {
workArray[i] = 2;
}
uint16_t position = 0;
uint16_t predigit = 0;
uint16_t nines = 0;
for (uint16_t digit = 0; digit < PI_DIGITS; digit++) {
uint32_t q = 0;
for (int i = WORK_SIZE; i > 0; i--) {
uint32_t x = 10UL * workArray[i - 1] + q * i;
workArray[i - 1] = x % (2 * i - 1);
q = x / (2 * i - 1);
}
workArray[0] = q % 10;
q /= 10;
if (q == 9) {
nines++;
}
else if (q == 10) {
appendDigit(predigit + 1, position);
while (nines > 0) {
appendDigit(0, position);
nines--;
}
predigit = 0;
}
else {
appendDigit(predigit, position);
predigit = q;
while (nines > 0) {
appendDigit(9, position);
nines--;
}
}
}
appendDigit(predigit, position);
piRaw[position] = '\0';
}
void setup()
{
Serial.begin(115200);
// U některých desek počká na otevření sériového portu
// maximálně dvě sekundy.
uint32_t serialStart = millis();
while (!Serial && millis() - serialStart < 2000) {
}
Serial.println();
Serial.println(F("Vypocet cisla pi na 40 desetinnych mist"));
Serial.println(F("---------------------------------------"));
uint32_t startTime = micros();
calculatePi();
uint32_t calculationTime = micros() - startTime;
Serial.print(F("pi = "));
// Přeskočení pomocné úvodní nuly
Serial.print(piRaw[1]);
Serial.print('.');
for (uint16_t i = 2; i <= PI_DIGITS; i++) {
Serial.print(piRaw[i]);
}
Serial.println();
Serial.print(F("Doba vypoctu: "));
Serial.print(calculationTime);
Serial.println(F(" us"));
Serial.print(F("Doba vypoctu: "));
Serial.print(calculationTime / 1000.0, 3);
Serial.println(F(" ms"));
}
void loop()
{
}