Scor­pio News

  

January–March 1987 – Volume 1. Issue 1.

Page 48 of 63

The accuracy of the results obtained with a computer depends critically upon the level of precision to which the language implementation does its calculations – so an engineering program (or any in which the accuracy of the result had to be guaranteed to a larger number of significant figures) would need to be structured so that the largest possible number of significant figures were employed and no ‘rounding off’ was applied. This can be achieved by the use of double precision arithmetic but there is a time and memory penalty for its use. On the other hand, a simple program to calculate the areas of triangles or to work out interest payment needs only single precision arithmetic – the default on most systems – and rounding off may be permissible. The use of built-in ‘trace’ of debugging facilities can allow intermediate results to be examined if final results are not as would be expected. One should not accept the computer’s results uncritically.

Resource efficiency

Two major constraints on most microcomputer users are the amount of directly addressable memory and the execution time of a program; a large adventure program may use most if what is available – this is not usually a problem but tends to slow things down a bit unless the code is efficient written. Multi-statement lines can help economize on memory usage but this should be employed with care if the legibility of the program is not to suffer. Some maths programs in which large arrays of data are processed may use lots of memory and take a long time to do it. In both cases, the use of memory and time can be optimized by cutting out unnecessary steps and optimizing where possible. A few examples may help illustrate this point.

It is quicker to use addition and subtraction rather than multiplication so X=Y+Y is faster than X=2*Y. Multiplication is faster than division so:

T=1/Z
U=X*T
V=Y*T
W=A*T

is faster than

U=X/Z
V=Y/Z
W=A/Z

Exponentiation is very slow, particularly when the ‘power’ used is a non-integer. It is a lot quicker to multiply:

T=X*X*X is quicker than T=X^3 (or T=X**3) and a lot quicker than T=X**3.0 which implies that a non-integer power is used (this does not apply to BASIC as far as I am aware).

Always use the supplied functions such as SQR or SQRT; the results are usually more accurate and more quickly obtained than if you devise your own version or use a fractional exponent! If you need to write your own functions (such as TAN which isn’t found in most Pascals or Fortrans) do check your version using suitable values and paper and pencil.

Inefficient programmers often repeat calculations within the same statement or loop:

A=B+C–X+4*(B+C)/Y   could run faster as two statements:

P=B+C
A=P–X+4*P/Y

Further savings in time can be gained by examining the way in which arrays are accessed. If an array element is used several times in a series of calculations, the value of the array element, can be assigned to a variable and this variable used in the calculations which follow:

Z=X(I,J)
A=B*Z
L=C*Z

is better than

A=B*X(I,J)
L=C*X(I,J)
Page 48 of 63