Issue
I'm working on an embedded system, and I have my own timer function (start_timer()
and stop_timer()
). I'm hoping to time the following code precisely, but keep the optimization (-O2
).
void loop() {
uint16_t fram_cnt = 0;
for (uint16_t i = 0; i < VEC_SIZE; i++) {
fram_cnt += (fram_dst[i] == i);
}
}
int main() {
init();
start_timer();
loop();
uint32_t time = stop_timer();
msp_send_printf("loop time: %n", time);
exit();
}
However, the value of time
is always 0 with -O2
on. My guess is that the compiler reorder my code because if I use -O0
optimization, I can get correct time, and my CPU doesn't have out-of-order execution.
How can I correctly benchmark my program while keeping the same optimization level?
Solution
Your function is being optimized out when you enable optimizations (as it does not have any observable effect) and the compiler will not even emit the call to it:
loop:
bx lr
.LC0:
.ascii "loop time: %n\000"
main:
push {r3, lr}
bl init
bl start_timer
bl stop_timer
mov r1, r0
ldr r0, .L5
bl msp_send_printf
movs r0, #1
bl exit
.L5:
.word .LC0
As a minimum remedy simple move the definition of the counter outside the function:
uint16_t fram_cnt = 0;
void loop() {
fram_cnt = 0;
for (uint16_t i = 0; i < VEC_SIZE; i++) {
fram_cnt += (fram_dst[i] == i);
}
}
and it will be called and executed: https://godbolt.org/z/TY1sG3nPj
Answered By - 0___________ Answer Checked By - Mildred Charles (WPSolving Admin)