Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:03:51+00:00 2026-06-08T04:03:51+00:00

Possible Duplicate: Is it faster to count down than it is to count up?

  • 0

Possible Duplicate:
Is it faster to count down than it is to count up?

For example,

for (int i = 0; i < max; i++)
{
    ...
}

and

for (int i = max-1; i >= 0; i--)
{
    ...
}

These two loops are essentially the same, and assuming that the loop doesn’t contain any array operation. However, for the first case, every iteration will require loading max into a register in the processor and then compare between i and max.. on the other hand, the latter case doesn’t require loading 0 into a register for that 0 is already there in a register, so that there is only a comparison for the latter loop. Please correct me if I am wrong, and elaborate if I am right.
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:03:52+00:00Added an answer on June 8, 2026 at 4:03 am

    The code represented by the ellipsis will almost certainly relegate any actual performance difference to mere noise. However, you’re not correct in all of your assumptions.

    every iteration will require loading max into a register in the processor and then compare between i and max

    Maybe, but probably not. This depends on your code, but any sane optimizing compiler will be able to detect if the counter is changing between iterations.

    I’m not sure where you got some of your ideas, but they are a bit misguided and don’t take into account how an optimizing compiler works. Look at your disassembly and see what the real difference is yourself. Oh what the hell, I’ll do it (it’s fun anyway):

    The program is:

    int main(int argc, char *argv[]){   
        int max = 10;
        for (int i = max-1; i >= 0; i--)
        {
            cout << i;
        }
        return 0;
    }
    

    The generated assembly (VS2010 release, comments my own) is:

    int main(int argc, char *argv[]){   
    00341000  push        esi  
        int max = 10;
        for (int i = max-1; i >= 0; i--)
    00341001  mov         esi,9               ; move a static 9 into esi
    00341006  jmp         main+10h (341010h)  
    00341008  lea         esp,[esp]           ; load the address of whatever
    0034100F  nop                             ; esp points to in memory 
        {                                     ; (not a memory fetch, just address calculation)
            cout << i;
    00341010  mov         ecx,dword ptr [__imp_std::cout (342048h)]  
    00341016  push        esi  
    00341017  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (342044h)]  
    0034101D  dec         esi                 ; decrement counter
    0034101E  jns         main+10h (341010h)  ; jump if not signed
        }
    

    And for the more idiomatic version…

    int main(int argc, char *argv[]){   
    00AC1000  push        esi  
        int max = 10;
        for (int i = 0; i < max; i++)
    00AC1001  xor         esi,esi  
        {
            cout << i;
    00AC1003  mov         ecx,dword ptr [__imp_std::cout (0AC2048h)]  
    00AC1009  push        esi  
    00AC100A  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0AC2044h)]  
    00AC1010  inc         esi               ; increment esi
    00AC1011  cmp         esi,0Ah           ; compare to 10 (0Ah)
    00AC1014  jl          main+3 (0AC1003h) ; if less, jump to top 
        }
    

    So yes, the first version uses a jns instruction (jump if not signed), so the comparison is simplified a bit (comparing to 0). It also contains a few more instructions, but no comparison.

    However, notice that the comparison made in version two is also static. It knows that max doesn’t change throughout the loop, so it can optimize that bit accordingly.

    But I would reiterate strongly that this is not likely to ever produce an appreciable performance benefit. Even the high performance timer on my Windows PC couldn’t give me a good statistical difference between the two because the call to cout takes soooo much longer than the loop instructions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is it faster to count down than it is to count up?
Possible Duplicate: Why is string concatenation faster than array join? Usually we need to
Possible Duplicate: Is shifting bits faster than multiplying and dividing in Java? .NET? To
Possible Duplicate: is “else if” faster than “switch() case” ? I've encountered a lot
Possible Duplicate: C/C++: is GOTO faster than WHILE and FOR? I know this has
Possible Duplicate: Is recursion ever faster than looping? I was first trained to program
Possible Duplicate: Is “for(;;)” faster than “while (TRUE)”? If not, why do people use
Possible Duplicate: Faster DirectoryExists function? I want to check if some file exists on
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc I
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.