Initially for both subscript and index, system has to calculate the location of a entry from the beginining of the table. The real difference starts showing up during subsequent usage of subscript and index. This can be illustrated using the below sample program
WORKING-STORAGE SECTION.
01 SUB1 PIC 9(02).
01 TAB1.
05 TAB1-ENTRY OCCURS 5 TIMES.
10 TAB1-ELEMENT PIC X(10).
01 TAB2.
05 TAB2-ENTRY OCCURS 5 TIMES INDEXED BY IX2.
10 TAB2-ELEMENT PIC X(10).
PROCEDURE DIVISION.
ACCEPT SUB1. 1
MOVE 'AA' TO TAB1-ELEMENT (SUB1) 2
SET IX2 TO SUB1 3
MOVE 'AA' TO TAB2-ELEMENT (IX2) 4
PERFORM PARA2 10 TIMES 5
STOP RUN. 6
PARA2. 7
MOVE 'AA' TO TAB1-ELEMENT (SUB1). 8
MOVE 'AA' TO TAB2-ELEMENT (IX2). 9
For statement 2(MOVE statement in PARA1), behind the scene following operations are carried out.
a. convert SUB1 to packed decimal
b. convert packed decimal value of SUB1 to BINARY
c. multiply the length of a table occurrence with the BINARY value got in previous step and this gives the offset of the table entry within the table
d. it uses the offset value to locate the table entry and populate it
Statements 3 and 4 also, carries out the same above steps.
For statement 8 which uses subscript, it again carries out the same operations carried out in statement-2. But for statement 9, it reloads the register with the offset (index) already stored in memory.
For subscript, it carries out the offset calculation again and again whereever the table entry is accessed. Where as in the case of index, after the calculation of offset, it stores the result in the memory. When the table is accessed again using index, then it retrives the offset which is already stored in memory to access the table, thus avoid the offset calculation.
For subscript, it carries out the offset calculation again and again whereever the table entry is accessed. Where as in the case of index, after the calculation of offset, it stores the result in the memory. When the table is accessed again using index, then it retrives the offset which is already stored in memory to access the table, thus avoid the offset calculation.
Now you know why index is faster than subscript.
Machine code generated for the above given program
000016 MOVE
00024C 5830 912C L 3,300(0,9) BLW=0
000250 F271 D0F8 3000 PACK 248(8,13),0(2,3) TS2=0 SUB1
000256 960F D0FF OI 255(13),X'0F' TS2=7
00025A 4F40 D0F8 CVB 4,248(0,13) TS2=0
00025E 1854 LR 5,4
000260 4C40 A018 MH 4,24(0,10) PGMLIT AT +16
000264 5A40 A010 A 4,16(0,10) PGMLIT AT +8
000268 1A43 AR 4,3
00026A D209 4008 A01A MVC 8(10,4),26(10) TAB1-ELEMENT() PGMLIT AT +18
000017 SET
000270 0650 BCTR 5,0
000272 4C50 A018 MH 5,24(0,10) PGMLIT AT +16
000276 5050 9134 ST 5,308(0,9) IX2
000018 MOVE
00027A 1A53 AR 5,3
00027C D209 5040 A01A MVC 64(10,5),26(10) TAB2-ELEMENT() PGMLIT AT +18
000019 PERFORM
000282 4100 000A LA 0,10(0,0)
000286 GN=8 EQU *
000021 *PARA2
000022 MOVE
000286 5820 912C L 2,300(0,9) BLW=0
00028A F271 D0F8 2000 PACK 248(8,13),0(2,2) TS2=0 SUB1
000290 960F D0FF OI 255(13),X'0F' TS2=7
000294 4F30 D0F8 CVB 3,248(0,13) TS2=0
000298 4C30 A018 MH 3,24(0,10) PGMLIT AT +16
00029C 5A30 A010 A 3,16(0,10) PGMLIT AT +8
0002A0 1A32 AR 3,2
0002A2 D209 3008 A01A MVC 8(10,3),26(10) TAB1-ELEMENT() PGMLIT AT +18
000023 MOVE
0002A8 5830 9134 L 3,308(0,9) IX2
0002AC 1A32 AR 3,2
0002AE D209 3040 A01A MVC 64(10,3),26(10) TAB2-ELEMENT() PGMLIT AT +18
0002B4 4600 B07A BCT 0,122(0,11) GN=8(000286)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.