DO loop instead of REPEAT

The DO loop constructs are much faster than the REPEAT loop (when you don't need the extra that REPEAT provides... if you don't know what the extra is, you probably don't need it).

You can have some fun confirming this with the following code (It doesn't take many loops to have execution times in seconds!):

DEFINE TEMP-TABLE aTempTable NO-UNDO
 FIELD cTest AS CHARACTER.

DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE cResults AS CHARACTER NO-UNDO INITIAL "".
DEFINE VARIABLE iMethodTime AS INTEGER NO-UNDO.
&SCOPED-DEFINE NumLoops  1000000
&SCOPED-DEFINE NumLoops2 100000

ETIME(TRUE).
REPEAT i = 1 TO {&NumLoops}:
END.
iMethodTime = ETIME.
cResults = cResults + "~n Method 1:" + STRING(iMethodTime).

ETIME(TRUE).
DO i = 1 TO {&NumLoops}:
END.
iMethodTime = ETIME.
cResults = cResults + "~n Method 2:" + STRING(iMethodTime).

/*test with record creation*/
ETIME(TRUE).
REPEAT i = 1 TO {&NumLoops2}:
  CREATE aTempTable.
END.
iMethodTime = ETIME.
cResults = cResults + "~n Method 1:" + STRING(iMethodTime).

EMPTY TEMP-TABLE aTempTable.
ETIME(TRUE).
DO i = 1 TO {&NumLoops2}:
  CREATE aTempTable.
END.
iMethodTime = ETIME.

cResults = cResults + "~n Method 2:" + STRING(iMethodTime).
MESSAGE cResults VIEW-AS ALERT-BOX.

A couple of results on my machine (btw, both methods are faster using 10.1C then using 10.1B or 10.1A...):
Method 1:437
Method 2:281 - 35% faster
Method 1:2641
Method 2:2109 - 20% faster


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Problem of Do loop

Did you know when trying to input from a file using a do loop there is an issue.
It will never execute any thing after that do loop.
Where as reading the same file using Repeat will work.


Repeat blocks also have a

Repeat blocks also have a counter associated with them (undo units or something like that IIRC), and they have an upper limit around 2G, so doing too many REPEAT iterations can crash a session.

REPEAT blocks come with certain frame iteration and TX handling / scoping features which are useful in appropriate places.

Bottom line - if there's no explicit need for what REPEAT provides, use DO WHILE.


The extras

And now for the record (or the really interested people) a summary of the extras can be found at ... ;-)


...and the extras are...

http://www.psdn.com/library/servlet/KbServlet/download/4818-102-14243/dv... (page 21-4)

Using DO instead of REPEAT

Back in Chapter 6, “Procedure Blocks and Data Access,” you learned about the various properties of different kinds of blocks. Remember that the DO block, by default, does not provide you with many of the default services that the REPEAT block does (for example, transaction management and default frame management). The flip side of this is that a DO block is much faster than a REPEAT block if you don’t need these services. So, use a simple DO block whenever possible for any kind of iterating block that doesn’t need to manage a transaction or iterate through a DOWN frame.

For a complete list of the differences, you need to look at chapter 6 of that book.

On overview of the different ABL block types can be found in the documentation book "Pocket Progress". Unfortunately, it appears to have only ever been produced in hard copy format :-(


The extras

In fact one really needs to know what the extras are when one wants to improve performance for existing code. If you change a repeat block to a do block that was deliberately written as repeat block because the extras it provides are required, changing that code could break it instead of improving it.


New code VS refactoring

Agreed. There's always a risk in optimizing existing code. You may be removing a side effect (or even bug patch) of a code construct that was necessary. Testing is mandatory. But when writing new code, the cost of using the fastest construct is often negligible.