Pages

Showing posts with label Assembler. Show all posts
Showing posts with label Assembler. Show all posts

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.

Tuesday, November 2, 2010

COBOL Performance Tuning - The INITIALIZE Statement

The INITIALIZE statement in COBOL is really a convenient statement.  It will move SPACES to alpha numeric fields and ZEROS to numeric fields.  But with this convenience comes a cost when initializing large copybooks.

Here is what happens when you initialize a copybook.  The INITIALIZE statement will initialize each field that is defined with a PIC clause, except FILLER fields.  FILLER fields will remain LOW-VALUES or nulls.  The underlying Assembler code that is created for each field being initialized is at least 3 Assembler statements.  So, if you have a copybook with 500 individual fields being initialized, that becomes at least 1500 Assembler statements that are created when the code is compiled.  These 1500 Assembler statements will be executed each time that INITIALIZE statement is executed.

Here are some simple options to tune code where an INITIALIZE needs to be done.

  • Do not initialize the copybook at all.  You will have to determine if this is an option.
  • If the copybook contains entirely alpha numeric fields that can be initialized as spaces, move SPACES to the 01 level.  This will generate 3 Assembler statements.
  • If the copybook contains a mixture of alpha numeric and numeric, and all of the numeric fields need to be initialized with ZEROS, then perform the following:
    01  BIG-RECORD.              COPY BIGREC.
    .

    .
    .
    PROCEDURE DIVISION
    .
    .
    .
        MOVE SPACES TO BIG-RECORD.
        INITIALIZE BIG-RECORD
          REPLACING NUMERIC DATA BY ZEROES.

  • The previous INITIALIZE statement will only initialize the numeric fields with ZEROS.  This would have had to be done either way, so here the INITIALIZE statement with the REPLACING clause will save you several lines of COBOL code, and will create the same amount of underlying Assembler code.
  • Your FILLER fields will contain SPACES when initialized using the 2nd and 3rd options.
When large arrays are present, there is another technique that can be used in conjunction with this way of initializing fields.  This will be discussed in a later entry.