Pages

Sunday, November 21, 2010

COBOL Performance Tuning - Using Conditional Statements

This performance tuning tip is one that can be applied to any language that you program in.

When you evaluate a field for more than one possible value, whether you use an IF, EVALUATE, GO TO DEPENDING ON, or some other conditional statement, check for the most common value first, then the next common, and so on.

For example, if you are reading through a file and you only want records that are in "A" Active, "W" Waiting or "P" Pending status, and you know out of the one million records you will be reading that about 900,000 of them are active, 30,000 are waiting, 20,000 are pending and the rest are something else, you will want to something like the following:

IF MY-STATUS = "A"
    PERFORM STATUS-IS-A THRU STATUS-IS-A-EXIT
    GO TO PARA-EXIT
ELSE
    IF MY-STATUS NOT EQUAL "W"AND
         MY-STATUS NOT EQUAL "P"
            GO TO PARA-EXIT
    ELSE
        IF MY-STATUS = "W"
            PERFORM STATUS-IS-W THRU STATUS-IS-W-EXIT
        ELSE
            PERFORM STATUS-IS-P THRU STATUS-IS-P-EXIT
        END-IF
        GO TO PARA-EXIT
    END-IF
    GO TO PARA-EXIT
END-IF.

I don't know how many programmers think about this scenario when they code, but I suspect many do not.

No comments:

Post a Comment