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.