On 26 Feb 2025, at 20:55, Moshe Braner <moshe....@gmail.com> wrote:Does anybody know of measurements of the time it takes the ESP32, using the standard libraries, to compute functions such as sin(), cos(), atan2() and hypot() at float (not double) precision? I tried to measure it myself and the result do not make sense. Trying to prevent compiler optimizations from short-cutting the loop is tricky.
--
You received this message because you are subscribed to the Google Groups "SoftRF_community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to softrf_communi...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/softrf_community/7daf975c-6842-4faa-83bb-097c5e9b0b8dn%40googlegroups.com.
I just tried on an ESP32-C3 and I measured cos at 20.3 uS.
To stop false optimization, you need to provide input values the
compiler doesn't know about, or are too complex to calculate at
compile time e.g.
The code I used was:
int value = analogRead(2);
float val = float(value) / 4095.0;
int64_t start_time = esp_timer_get_time(); // Get time in
microseconds
float total = 0.0;
for (int i=0; i<1000; i++) {
total = total + (val * float(i)/10000.0); // A
//total = total + cos(val * float(i)/10000.0); // B
}
int64_t elapsed_time = esp_timer_get_time() - start_time;
Serial.printf("Total: %f\n", total);
Serial.printf("Elapsed time: %lld us\n", elapsed_time);
delay(500);
The output needs to go somewhere otherwise the compiler will treat it as unused and remove what it considers to be 'dead code'.
I ran with with A to get a baseline and then with B to measure cos.
Rob
To view this discussion visit https://groups.google.com/d/msgid/softrf_community/BB08B044-9E53-4890-80C6-E52C90D38006%40gmail.com.