Yesterday, my colleague asked me a question. There are two loop statements:
For (I = N; I> 0; I --)
{
...
}
For (I = 0; I <n; I ++)
{
...
}
Why is the former faster than the latter?
My explanation at the time was:
I -- the operation itself will affect CPSR (currentProgramStatus Register), CPSR common signs are N (result is negative), Z (result is 0), C (input), O (overflow ). I> 0, which can be determined directly by the Z mark.
The I ++ operation also affects CPSR (the current program status register), but only affects the O (with overflow) flag, which does not help the I <n judgment. Therefore, an additional comparison command is required, that is, each loop needs to execute one more command.
(This is what tjww told me five years ago. At that time, he wrote an LCD driver on the AVR, and the latter will flash, so there is no problem with the former .)
To confirm that my understanding is correct, I did an experiment:
Int loop_dec (int n)
{
Int I = 0;
Int V = 0;
For (I = N; I> 0; I --)
V + = I;
Return V;
}
Int loop_inc (int n)
{
Int I = 0;
Int V = 0;
For (I = 0; I <n; I ++)
V + = I;
Return V;
}
Compile with arm-Linux-GCC and then disassemble:
I -- cycle condition:
4c: e51b3014 LDR R3, [FP, #-20]
50: e3530000 CMP R3, #0; 0x0
54: cafffff5 Bgt 30
I ++ cycle conditions:
B8: e51b3018 LDR R3, [FP, #-24]
BC: e1520003 CMP R2, R3
C0: bafffff4 BLT 98
The results are not the same as I thought. What is the problem? I think the result may be changed:
I -- cycle condition:
14: e2500001 subs r0, R0, #1; 0x1
18: 1 afffffc BNE 10
I ++ cycle conditions:
3c: e2833001 add R3, R3, #1; 0x1
40: e1500003 CMP r0, r3
44: 1 afffffb BNE 38
That's right. One CMP command is missing.
-- Reposted from Miss Li xianjing's blog