Previous Topic Next topic Print topic


Optimizing Arithmetic Statements

Arithmetic statements, for which simple two-operand statements are the most efficient.

For the best performance from arithmetic statements:

Use simple two-operand arithmetic statements wherever possible. The following operations are optimized for COMP-5 and COMP-X data items up to four bytes long:

 MOVE a TO b 
 ADD a TO b 
 SUBTRACT a FROM b 
 MULTIPLY a BY b 
 DIVIDE a INTO b 
 IF a condition b

where:

a Is a numeric literal value or data item up to four bytes long

b Is a numeric data item up to four bytes long

On other data items, these simple operations result in faster code than more complex instructions, but the benefits are not as great as with COMP-5 or COMP-X items. More complex forms of these instructions, involving more than two operands, might not produce code which is as efficient as the simple form.

Do not use the GIVING form of the above verbs. If necessary, create temporary data items and code several simple statements to achieve the same result. For example, write:

 MOVE a TO c
 ADD b TO c

rather than:

 ADD a TO b GIVING c

Provided that the operands are all of the same type, statements that contain the GIVING phrase are optimized.

Do not use the REMAINDER, ON SIZE ERROR or CORRESPONDING phrases.

When using the simple operations described above on COMP-5 or COMP-X items, do not use the ROUNDED phrase. In other situations, although the ROUNDED phrase impacts performance, but it is generally faster to use ROUNDED than try to round the result using your own routine.

Do not mix items of different sizes in an arithmetic statement (for example, try to use all two-byte items or all four-byte items).

Previous Topic Next topic Print topic