Espidf vs arduino framework performance

Hello everyone,

I have successfully implemented a very simple neural network on an ESP32. Initially, I used the ESP-IDF framework as it's highly customizable and allowed me to tweak the sdkconfig to minimize energy consumption. However, after testing the same code using the Arduino framework, I observed a significant improvement in performance almost a 10x factor.

Now, I'm attempting to understand why there's such a significant discrepancy between the two frameworks despite using the same code. My initial guess is that the Arduino framework might be applying certain optimizations at the compilation stage that are not made with the ESP-IDF framework.

Here's the simple code I ran to test the performance of both frameworks:

#define N 1000
#define ITER 10000
#define INPUT_LAYER_SIZE 13

float X[N][INPUT_LAYER_SIZE];
float weights[INPUT_LAYER_SIZE] = {0};

int64_t startTime, endTime;


void setup() {

    srand(42);

    for(int i = 0; i < N; i++) {
        for(int j = 0; j < INPUT_LAYER_SIZE; j++) {
            X[i][j] = (float)rand() / (float)RAND_MAX;
        }
    }

    for(int i = 0; i < INPUT_LAYER_SIZE; i++) {
        weights[i] = (float)rand() / (float)RAND_MAX;
    }

    startTime = esp_timer_get_time();
    
    float weightedSum = 0;
    for (int iter = 0; iter < ITER; iter++) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < INPUT_LAYER_SIZE; j++) {
                weightedSum += weights[j] * X[i][j];
            }
        }
    }

    endTime = esp_timer_get_time();

    printf("execution time (in seconds): %.4f\n", (endTime - startTime) / 1000000.0);

    printf("weightedSum : %f\n", weightedSum);
}

In terms of execution time, ESP-IDF took 18.960 seconds, whereas with the Arduino framework, it only took 3.6903 seconds.

Given that I am using the same code on the same hardware, can anyone shed some light on why the Arduino framework outperforms the ESP-IDF framework so significantly? Also, is there a way to get the same level of performance on the ESP-IDF as the Arduino IDE?

Looking forward to your insights. Thank you.

I see that you're using floats - could it be that ESP-IDF is doing that in software, and Arduino in hardware...?
:thinking:

Thank you for your input. I have indeed checked whether CONFIG_SOC_CPU_HAS_FPU is enabled, and it already was, so that does not appear to be the root of the problem.

After some further testing, I've discovered that the issue seems to come from compiler flag optimization. When using -O2 with ESP-IDF, I've managed to reduce the processing time from 18.96s to 5.66s. It's still not as fast as the Arduino framework out of the box (3.69s).

1 Like

You need to provide more information. For example, versions of both frameworks. In the case of ESP-IDF already provides the SDK config file. At first glance the problem with compiler optimizations, CPU frequency, frequency of flash.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.