I think the compiler today can identify this optimization and thus the final
code will be the same.

You can check the assembly code generated by the gcc compiler for both the
loops.

On Wed, Nov 24, 2010 at 4:29 PM, shiva <shivanand.kadwad...@gmail.com>wrote:

> After googling i came to know that comparison with 0 takes less time
> than comparing with some other number.So decremental loop is
> fast(difference is very very low)
>
> On Nov 22, 5:42 pm, rahul patil <rahul.deshmukhpa...@gmail.com> wrote:
> > Might be its is due to the comparison operation which takes many cycles
> >
> > i < max takes more cycles of cpu than just simply checking i
> >
> > On Sun, Nov 21, 2010 at 6:50 PM, 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<algogeeks%2bunsubscr...@googlegroups.com>
> <algogeeks%2bunsubscr...@googlegroups.com<algogeeks%252bunsubscr...@googlegroups.com>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/algogeeks?hl=en.
> >
> > --
> > Regards,
> > Rahul Patil
>
> --
> 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<algogeeks%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Regards,
Rishi Agrawal

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

Reply via email to