Hi Matthias;
I know you probably put as simple an example together to demonstrate a really great feature of Natural.
However, programmers tend to cut and paste code from this forum and SAG-L, hence words of warning should be posted as well. In this case, it should be pointed out that you probably should almost never use SUBSTRING when the last two arguments are constants. The reason is quite simple, REDEFINE is far faster than SUBSTRING.
I said almost never. If I am many pages deep in a Natural program, and I need such functionality, and, this is a one time use of the functionality, and I am too lazy to go back to the LDA to add the REDEFINE, I use SUBSTRING.
BUT, if this functionality is inside a loop processing a file with 10 million records, …
well the following example shows the performance cost:
DEFINE DATA LOCAL
1 #A (A5)
1 REDEFINE #A
2 FILLER 2X
2 #B (A2)
1 #LOOP (I4)
1 #CPU-START (I4)
1 #CPU-ELAPSED (I4)
END-DEFINE
*
INCLUDE AATITLER
INCLUDE AASETC
*
MOVE *CPU-TIME TO #CPU-START
SETA. SETTIME
FOR #LOOP = 1 TO 1000000
IF SUBSTRING (#A,3,2) = ‘QQ’
IGNORE
END-IF
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 5T ‘SUBSTRING TIME’ #CPU-ELAPSED *TIMD (SETA.) //
*
MOVE *CPU-TIME TO #CPU-START
SETB. SETTIME
FOR #LOOP = 1 TO 1000000
IF #B = ‘QQ’
IGNORE
END-IF
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 5T ‘REDEFINE TIME’ #CPU-ELAPSED *TIMD (SETB.) //
*
MOVE *CPU-TIME TO #CPU-START
SETC. SETTIME
FOR #LOOP = 1 TO 1000000
IGNORE
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 5T ‘FOR TIME’ #CPU-ELAPSED *TIMD (SETC.)
END
PAGE # 1 DATE: 10/16/10
PROGRAM: SUBS01 LIBRARY: INSIDE
SUBSTRING TIME 77 8
REDEFINE TIME 47 5
FOR TIME 26 2
After subtracting out the FOR loop times, the Elapsed times are 51 for SUBSTRING and 21 for REDEFINE; the CPU times are 3 versus 6. Either way, at least two to one.
If the definitions are changed a bit, say to:
1 #A (A500)
1 REDEFINE #A
2 FILLER 420X
2 #B (A2)
The REDEFINE times, as one would expect, do not change. The SUBSTRING times increase substantially.
steve
aka The CPU policeman