Pages

Showing posts with label COBOL OCCURS. Show all posts
Showing posts with label COBOL OCCURS. Show all posts

Sunday, November 14, 2010

COBOL Performance Tuning - INITIALIZE Statement And Large Arrays

We have covered the INITIALIZE statement and initializing large arrays, so now let's put them together.

Say you have a large copybook with a large array in it.

01  LARGE-RECORD.
(several 05 levels)
    05  MY-TABLE.
        10  TABLE-ENTRY OCCURS 999 TIMES.
(multiple fields here)
(more 05 levels)

Using the above copybook, you would have to modify it so that you can initialize parts of the copybook, but not the array, as you want to use a separate routine to do that.  Here is what I would do:

01  LARGE-RECORD.
  03  LARGE-RECORD-PART-1.
(several 05 levels)
  03  LARGE-RECORD-PART-2.
    05  MY-TABLE.
        10  TABLE-ENTRY OCCURS 999 TIMES.
(multiple fields here)
  03  LARGE-RECORD-PART-3
(more 05 levels)

Now you can initialize LARGE-RECORD like this:

MOVE SPACES TO LARGE-RECORD.
INITIALIZE LARGE-RECORD-PART-1
    REPLACING NUMERIC DATA BY ZEROS.
INITIALIZE LARGE-RECORD-PART-3
    REPLACING NUMERIC DATA BY ZEROS.

You would then initialize MY-TABLE as discussed in COBOL Performance Tuning - Large Arrays.

Contact me if you know of other ways (especially better ones) of accomplishing this objective.

Friday, November 5, 2010

COBOL Performance Tuning - Large Arrays

When initializing large arrays with default values, most programmers will perform a paragraph or internal loop, moving the default values to each field of each occurs until all of the occurrences are populated.  This is not necessarily the most efficient way to perform this task.

One way to perform this task more efficiently is to move your default values to the fields of the first occurrence.  Then move that first group of occurs to the second group (as a block, not as individual fields).  Then move the first two group of occurs to the third and fourth.  Then move the first four group of occurs to the next four, and so on and so on.  Be careful when you get to the end of your occurs that you do not move data beyond the end of your array, or you will likely clobber code.

Here is a quick example:

01  EXAMPLE-TABLE.
    05  MY-TABLE.
        10  TABLE-ENTRY OCCURS 999 TIMES.
            15  FIRST-NAME         PIC X(15).
            15  LAST-NAME          PIC X(15).
            15  SEX-CODE           PIC X.
            15  DOB.
                20  DOB-YYYY       PIC 9(4).
                20  DOB-MM         PIC 99.
                20  DOB-DD         PIC 99.
            15  SSN                PIC 9(9).
            15  SALARY             PIC S9(9)V99 COMP-3.
...
    MOVE SPACES TO MY-TABLE.
    MOVE ZEROS TO DOB
                  SSN
                  SALARY.
    MOVE MY-TABLE (1:54)
      TO MY-TABLE (55:54).
    MOVE MY-TABLE (1:108)
      TO MY-TABLE (109:108).
    MOVE MY-TABLE (1:216)
      TO MY-TABLE (217:216).

You obviously do not want to hard code like in the example above, but this shows the concept in a simple form. You would want to make your start position and length variable fields and perform this in a loop. How you code it is up to you.

The routine I am familiar with became more efficient than the typical performed loop at about 15 occurs. Yours may differ. You may have to look at the assembler code generated to make that determination. 

If anyone has other techniques to populate large arrays, please share them in a comment or email the concept to me and I'll post it on this blog.