Embedded C/C++

Moderators: Lundin
Number of threads: 209
Number of posts: 392

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
which one loop is faster and why? Posted by sandeep3047 on 21 Jan 2011 at 10:51 AM


which one is faster

1. for(i=0;i<num;i++)
2. for(i=num;i>0;i--)
assume that no code optimization and microprocessor has flags
Report
Re: which one loop is faster and why? Posted by AsmGuru62 on 22 Jan 2011 at 6:20 AM
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.
Report
Re: which one loop is faster and why? Posted by sandeep3047 on 23 Jan 2011 at 12:07 AM
but answer is given that 2nd loop is fast..i don't why there is some condition given regarding flags..
Report
Re: which one loop is faster and why? Posted by bazzynga on 12 May 2011 at 2:25 PM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.