Here are the results (my PC is a Pentium 4 (HT) with 3GHz, and NAT63) with your new program:
separate: 7 69
for loop: 23 225
dummy for loop: 0 5
If you change the string to:
FIELD#1 := ’ 1’ /* blank 1
separate: 6 56
for loop: 5 51
dummy for loop: 0 5
If you change the string to:
FIELD#1 := ‘1’ /* just a number
both algorithm fail, because
MOVE SUBSTRING (FIELD#1,1,#POS) TO #OUT1
or
#OUT1 := SUBSTR(FIELD#1,1,#LOOP2)
… the upperbound is 0 (#POS or #LOOP2)
If you change the string to:
FIELD#1 := ‘A’
your algorithm fail, because
you don’t find any number: #POS is 0
and therefore out of range…
MOVE SUBSTRING (FIELD#1,#POS) TO #OUT2
If you change the string to:
FIELD#1 := ‘---------------------------------------9’
your algorithm fail, because
#ARRAY (A30/1:20)
has to be changed to (A40/1:2)
… and everything changes if you use dynamic variables and *length(FIELD#1) as upper bound for the loop …
here the modified one …
DEFINE DATA LOCAL
1 FIELD#1 (A40)
*
1 #ARRAY (A40/1:2)
1 #POS (I4)
*
1 #OUT (A40)
1 #OUT1 (A40)
1 #OUT2 (A40)
1 #CPU-TIME (P9)
1 #CPU-START (P9)
1 #LOOP (I4)
1 #LOOP2 (I4)
END-DEFINE
* FIELD#1 := 'ATTN: TOM GROSS 3601 S 48 ST '
* FIELD#1 := 'A'
FIELD#1 := '9'
* FIELD#1 := '---------------------------------------9'
*
MOVE *CPU-TIME TO #CPU-START
SETA. SETTIME
FOR #LOOP = 1 TO 100000
RESET INITIAL #OUT1 #OUT2
#OUT1 := FIELD#1
SEPARATE FIELD#1 INTO #ARRAY (1:2) IGNORE
WITH RETAINED DELIMITERS '0123456789'
EXAMINE FIELD#1 FOR #ARRAY (2) GIVING POSITION #POS
IF #POS NE 0
MOVE SUBSTRING (FIELD#1,#POS) TO #OUT2
END-IF
SUBTRACT 1 FROM #POS
IF #POS > 0 /* changed after 1st post
MOVE SUBSTRING (FIELD#1,1,#POS) TO #OUT1
END-IF
END-FOR
*
PRINT FIELD#1
PRINT #OUT1 "/" #OUT2
COMPUTE #CPU-TIME = *CPU-TIME - #CPU-START
WRITE 'separate:' *TIMD (SETA.) #CPU-TIME
************************************
MOVE *CPU-TIME TO #CPU-START
SETB. SETTIME
FOR #LOOP = 1 TO 100000
RESET INITIAL #OUT1 #OUT2
#OUT1 := FIELD#1
FOR #LOOP2 = 1 TO 40
IF SUBSTR(FIELD#1,#LOOP2,1) IS (N1)
#OUT2 := SUBSTR(FIELD#1,#LOOP2)
ADD -1 TO #LOOP2
IF #LOOP2 > 0
#OUT1 := SUBSTR(FIELD#1,1,#LOOP2)
END-IF
ESCAPE BOTTOM
END-IF
END-FOR
END-FOR
PRINT FIELD#1
PRINT #OUT1 "/" #OUT2
COMPUTE #CPU-TIME = *CPU-TIME - #CPU-START
WRITE 'for loop:' *TIMD (SETB.) #CPU-TIME
*************************************
*
END