Okay, here is a PC timing comparison of two approaches
DEFINE DATA LOCAL
1 IP (A50) INIT <‘2797 SPRINGHILL DR ALBANY, OR, 23456’>
1 #PIECES (A15/1:20)
1 OP1 (A50)
1 OP2 (A50)
1 #INDEX (I2)
1 #COMMA (I2)
1 #BLANK (I2)
1 #OP2LENGTH (I2)
1 #LOOP (I4)
1 #CPU-START (I4)
1 #CPU-ELAPSED (I4)
END-DEFINE
*
MOVE CPU-TIME TO #CPU-START
SETA. SETTIME
FOR #LOOP = 1 TO 10000
SEPARATE IP INTO #PIECES () WITH RETAINED DELIMITERS
EXAMINE #PIECES (*) FOR ‘,’ GIVING INDEX #INDEX
MOVE #PIECES (#INDEX - 1) TO OP2
COMPRESS #PIECES (1:#INDEX - 2) INTO OP1
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE ‘separate ip’ *TIMD (SETA.) #CPU-ELAPSED
MOVE *CPU-TIME TO #CPU-START
SETB. SETTIME
FOR #LOOP = 1 TO 10000
EXAMINE IP FOR ‘,’ GIVING POSITION #COMMA
EXAMINE DIRECTION BACKWARD SUBSTRING (IP,1,#COMMA) FOR ’ ’ GIVING POSITION #BLANK
MOVE SUBSTRING (IP,1,#BLANK) TO OP1
ADD 1 TO #BLANK
COMPUTE #OP2LENGTH = #COMMA - #BLANK
MOVE SUBSTRING (IP,#BLANK,#OP2LENGTH) TO OP2
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE ‘examine ip’ *TIMD (SETB.) #CPU-ELAPSED
END
Page 1 07-11-26 08:16:44
separate ip 7 66
examine ip 2 19
On the mainframe I think the second approach will look even better since the SUBSTRING facility there was recently rewritten and is substantially more efficient than on the PC.