Wiki Home

Traversing Text


Namespace: SoftwareEng
Occasionally you must traverse a block of text, line-by-line, probably to parse information or do some other text-oriented task. Do you know the fastest way to do this in VFP?

The code below gives me the following results. Conclusion: In VFP6 using ALINES() and then traversing the array kicks the hell out of using MLINE() even if you use the _MLINE trick. ALINES() rocks. (See Topic: ALINES)
Results
Using MLINE()  22.321
Using MLINE() with _MLINE 0.471
Using ALINES() and traverse 0.010 <== Fastest 
Using ALINES() and FOR EACH 0.021

The Test
LOCAL lcString, i, y, z, k
LOCAL ARRAY laArray[1]
SET MEMOWIDTH TO 8000
lcString=filetostr(home()+"Genhtml.prg")  && 767 lines

*-- Test 1 -- MLINE
z=Seconds()
FOR i= 1 TO MEMLINES(lcString)
 y=MLINE(lcString,i)
ENDFOR
?"Using MLINE()", Seconds()-z

*-- Test 2 -- MLINE and _MLINE
z=Seconds()
_mline=0
FOR i= 1 TO MEMLINES(lcString)
 y=MLINE(lcString,1,_MLINE)
ENDFOR
?"Using MLINE() with _MLINE", Seconds()-z

*-- Test 3 -- ALINES() then traverse
z=Seconds()
FOR i= 1 TO ALINES(laArray,lcString)
 y=laArray[i]
ENDFOR
?"Using ALINES() and traverse", Seconds()-z

*-- Test 4 -- ALINES() and FOR EACH
z=Seconds()
ALINES(laArray,lcString)
FOR EACH k in laArray
 y=k
ENDFOR
?"Using ALINES() and FOR EACH", Seconds()-z

You can take that one step further by calling alines only once in the code.
On my machine, this "Test 3b" ran in one third the time of "Test 3" above

*-- Test 3b -- ALINES() then traverse with only one call to alines
z=Seconds()
n=ALINES(laArray,lcString)
FOR i= 1 TO n
 y=laArray[i]
ENDFOR
?"Using ALINES() and traverse with only one call to alines", Seconds()-z


Christopher Olson

Christopher - you are seeing some other memory or system busy state causing the difference between 3 and 3b. The FOR loop only executes the expression for the final value one time when the loop is first entered. If you run the test multiple times, without doing Test 1, you'd find no measurable difference between 3 and 3b. A good way to verify this would be to k = alines( laArray, lcString ) inside the loop, which makes it execute 295 times slower (but even this is still 5 times faster than Test 1). -- ?df

*-- Test 3c -- multiple calls to ALINES()
z=Seconds()
n=ALINES(laArray,lcString)
FOR i= 1 TO n
 k = alines( laArray, lcString )
 y=laArray[i]
ENDFOR
?"Using ALINES() and traverse with multiple call to alines", Seconds()-z


David - Those few extra hundredths of a second appear to have been coming from the test program's first call to ALINES(). I was not aware of that behavior of FOR..ENDFOR. Thanks for setting me straight.

Contributors: Steven Black Christof Wollenhaupt David Frankenbach
Category Performance Category VFP Functions
( Topic last updated: 1999.11.21 04:45:29 AM )