I am using ESP32 Pico Kit with Arduino Ide. I want to measure operation cycle for a few commands.
The code is so simple as below:
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long count = 10000;
unsigned long i = 0;
unsigned long _start;
unsigned long _end;
_start = ESP.getCycleCount();
while(i < count)
{
i++;
}
_end = ESP.getCycleCount();
Serial.println(_end - _start);
}
When I run the code, the result is 13.
Then, I changed the counter variable to different values and ran it again (values: 10, 100, 1000, 10000, ...).
The result is always 13!
Why the execution cycle does not change and it seems that the code is compiled as just one loop and the loop count is not important?
Am I missing something?
i=count. The compiler tries to optimize your code.volatilekeyword in theivariable declaration ....volatile unsigned long i = 0;iandcountaltogether. Or at least any decent optimizing compiler would do so.