A2. The reduce count cycle is better than the increase count cycle.

Source: Internet
Author: User

The condition for loop termination should be count to zero (count dawn to zero), rather than count to a specific limit value. Since the result of counting Subtraction is stored in the condition flag, the commands compared with zero can be omitted. Because 'is no longer used as the subscript index of the array, there is no problem in using the reduce count.

Int checksum (int * Data)
{
Unsigned int I;
Int sum = 0;
For (I = 64; I! = 0; I --)
{
Sum + = * (Data ++ );
}
Return sum;
}

For the unsigned cyclic Count value I, the condition for loop continuation can be either I! = 0 can also be I> 0. Because I cannot be a negative number, the two conditions are equivalent. For a signed cyclic Count value, using the condition I> 0 as the condition for continuing the loop is an adventure. The compiler does not need to allocate a register to save the cyclic termination value. commands compared with 0 can also be omitted.

All in all, I should be used for both signed and unsigned cyclic count values! = 0 is the end condition of the loop. For the signed number I, this is one less command than the condition I> 0.



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.