which one loop is faster and why? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

which one loop is faster and why?

which one is faster

1. for(i=0;i0;i--)
assume that no code optimization and microprocessor has flags


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Technically speaking these for loops will NOT do the same work, because index has different values (i will never be EQUAL to num in loop #1, but in loop $2 it WILL). The performance in FOR() part should be the same.[/color]
  • sandeep3047sandeep3047 Posts: 2Member
    but answer is given that 2nd loop is fast..i don't why there is some condition given regarding flags..
  • bazzyngabazzynga Posts: 1Member
    As per the dis-assembly in the 1st for loop, an additional instruction is required to set the flag if index has reached the 'num' value. Where as in the 2nd for loop, the index can be directly checked if it's greater than zero.
Sign In or Register to comment.