This of course depends on the processor, the compiler, and the optimization settings you choose.
On some processors like x86, 2 might compile to slightly smaller code because the decrement instruction instruction also sets the condition codes, and the conditional branch can use this result. On the other hand, i<max must compile to a separate comparison instruction. However, compiling to slightly smaller code doesn't mean the result is better speed. Anyway, it would be silly to choose the loop style for this reason. The difference of one instruction is just about never as important as making your code readable. Since the for (i = 0; i < n; i++) idiom is the overwhelmingly common way to write a loop that executes n times, this is the one you want unless there is a clear reason to use something else, such as a need to process an array from end to start. On Nov 21, 8:20 am, shiva <shivanand.kadwad...@gmail.com> wrote: > I want to know is there any difference between following two loop in > terms of speed. > > 1. > for(i=0;i<max;i++) > { > > //Some operation} > > 2. > for(i=max; i; i--) > { > > /Some operation > > } > > I heard second one is faster but don't have any proof > > Please comment. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.