I ++ loop and I-loop execution efficiency

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.