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!

Categories

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

Comments

  • 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.