We had a situation where we loaded a large VSAM file into memory to be searched by online CICS application. We encountered problems trying to emulate the VSAM START verb using COBOL SEARCH ALL verb when the key does not exist in the array. To get around this we had to write our own BINARY SEARCH in COBOL.
The array which we are searching contains about 240,000 entries.
We found that the hand coded BINARY SEARCH was significantly more efficient in terms of CPU usage than the COBOL "SEARCH ALL" verb.
The code was modeled after KNUTH's binary search, see this link
http://www.z390.org/contest/p21/P21DW1.TXT
the key to maximizing the performance of the code below was to use PIC S9(8) COMP for all the binary fields. This avoids the check for binary overflow in the generated code. Also we used the compiler option TRUNC(OPT) to avoid the code generated to check for truncation.
Note --> the IF statement after the PERFORM loop is to check for a "not found" condition. To emulate the VSAM START command we need to position the INDEX to the first row which is >= the search key.
MOVE +1 TO BIN-LOW.
MOVE BCNTR-TCA1-ENTRIES-USED
TO BIN-HIGH.
PERFORM WITH TEST AFTER
UNTIL BIN-LOW > BIN-HIGH
COMPUTE BIN-RANGE = BIN-HIGH - BIN-LOW
COMPUTE BIN-MID = (BIN-RANGE / 2) + BIN-LOW
SET TCA1-INDEX TO BIN-MID
EVALUATE TRUE
WHEN TCA1-KEY (TCA1-INDEX) = SRCH-TCA1-KEY
MOVE 1 TO BIN-RANGE
COMPUTE BIN-LOW = BIN-HIGH + 1
WHEN TCA1-KEY (TCA1-INDEX) < SRCH-TCA1-KEY
COMPUTE BIN-LOW = BIN-MID + 1
WHEN OTHER
COMPUTE BIN-HIGH = BIN-MID - 1
END-EVALUATE
END-PERFORM.
IF TCA1-KEY (TCA1-INDEX) < SRCH-TCA1-KEY
SET TCA1-INDEX UP BY +1
END-IF.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.