Grouping duplicate computations
When components of different expressions are duplicates, ensure that the compiler is able to optimize them. For arithmetic expressions, the compiler is bound by the left-to-right evaluation rules of COBOL. Therefore, either move all the duplicates to the left side of the expressions or group them inside parentheses.
About this task
If V1
through V5
are
variables, the computation V2 * V3 * V4
is a duplicate
(known as a common subexpression) in the following two statements:
COMPUTE A = V1 * (V2 * V3 * V4)
COMPUTE B = V2 * V3 * V4 * V5
In the following example, V2 + V3
is
a common subexpression:
COMPUTE C = V1 + (V2 + V3)
COMPUTE D = V2 + V3 + V4
In the following example, there is no common subexpression:
COMPUTE A = V1 * V2 * V3 * V4
COMPUTE B = V2 * V3 * V4 * V5
COMPUTE C = V1 + (V2 + V3)
COMPUTE D = V4 + V2 + V3
The optimizer can eliminate duplicate computations. You do not need to introduce artificial temporary computations; a program is often more comprehensible and faster without them.