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


  • [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]
  • but answer is given that 2nd loop is fast..i don't why there is some condition given regarding flags..
  • 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.

Howdy, Stranger!

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