[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


writing fast loop?

Posted on 2004-11-08
Medium Priority
Last Modified: 2008-02-01
Dear all,

There are many ways to write a for loop:

mov ecx, maxValue

loop: _StartOfLoop

xor eax,eax


add eax,1
cmp eax, maxValue
jnl _StartOfLoop

mov ecx, maxValue

djnz: _StartOfLoop


My Questions are:
1) Is there any other ways that I have missed out?
2)which is the fastest and why? (assume that the value of the counter i would be used in calculation)

thank you.

Question by:hengck23
LVL 22

Expert Comment

ID: 12532504
If you count down to zero you don't need a cmp instruction.

The loop instruction is slower than an explicit "dec ecx; jnz" pair on quite a few CPU's.

You can minimize the loop overhead by repeating the code inside the loop a few times and adjusting the loop count.


Accepted Solution

BeyondWu earned 1500 total points
ID: 12532710
Assume maxValue is an immediate value, and assume this code run on 486
mov ecx, maxValue                  ; 1 clocks
loop: _StartOfLoop                   ; 6 Clocks
so this mathod is 7 clocks

xor eax,eax                             ; 1 clock
add eax,1                               ; 1 clock
cmp eax, maxValue                 ; 1 clock
jnl _StartOfLoop                      ; 3 clocks
this method is 6 clocks

mov ecx, maxValue                  ; 1 clock
dec ecx                                   ; 1 clock
djnz: _StartOfLoop                  ; 3 clocks
this method is 5 clocks
LVL 85

Expert Comment

ID: 12539548
>  You can minimize the loop overhead by repeating the code inside the loop a few times and adjusting the loop count.
Up to the size of your instruction cache

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question