Measure Execution Time of Code in Seconds using C++ (Arduino)

millis function

#include <Arduino.h>

void doSomething(int seconds)
{
    delay(seconds * 1000);
}

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    while (true) {
        ulong startTime = millis();
        doSomething(1);
        ulong endTime = millis();

        double executionTime = (endTime - startTime) / 1000.0;
        Serial.printf("%.8f sec\n", executionTime);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.