Programmer Guide > Tips for Efficient Programming > Increasing Program Speed
  

Increasing Program Speed
The order in which an expression is evaluated can have a large effect on program speed. Consider the following statement, where A is an array:
; Scale A from 0 to 16.
B = A * 16. / MAX(A)
This statement first multiplies every element in A by 16, and then divides each element by the value of the maximum element. The number of operations required is twice the number of elements in A. This statement took 24 seconds to execute on a 512-by-512 single-precision floating-point array. A much faster way of computing the same result is:
; Scale A from 0 to 16 using only one array operation.
B = A * (16. / MAX(A))
or:
; Operators of equal priority are evaluated from left to right. 
; Only one array operation is required.
B = 16. / MAX(A) * A
The faster method only performs one operation for each element in A, plus one scalar division. It took 14 seconds to execute on the same floating-point array as above.

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.