Dynamic Arrays

A dynamic array is stored internally as a single string of characters. Whenever an element is accessed, this string has to be scanned, counting the system delimiters, to find the required data. Therefore, if the dynamic array is large, or is accessed a large number of times, a considerable amount of time is spent scanning this string. By careful programming the amount of string scanning can be minimised, resulting in an improvement in performance.

Dynamic vs Dimensioned Arrays

A dimensioned array stores each element as a separate variable in the descriptor table, therefore access to elements of a dimensioned array is considerably faster than to those of a dynamic array. Consider using a dimensioned array rather than a dynamic array when:

Note

  • Use MATREAD to read items into a dimensioned array, and MATWRITE to write from dimensioned arrays to items. Use the smallest dimension that will allow easy access to the attributes that will be repeatedly accessed. If the number of attributes is larger than the dimensioned size, the last element will contain a dynamic array of the remaining attributes.

  • Use MATBUILD and MATPARSE to convert between dimensioned and dynamic arrays so as to use dimensioned arrays whenever the number of accesses to the array is larger than the number of elements in the array.

Sequential Access

If a dynamic array is sequentially parsed, use the REMOVE statement rather than dynamic array references to avoid repeatedly scanning the array. This can be thousands of times faster in extreme cases!

If a dynamic array only contains attribute marks, each attribute can be extracted into the variable ELEMENT by the simple construct:

LOOP
 REMOVE ELEMENT FROM ARRAY SETTING TERM
WHILE TERM DO
 . . .
  process
  . . .
REPEAT

When the dynamic array contains other system delimiters, each attribute can be extracted into the variable ELEMENT using the construct:

LOOP
 REMOVE ELEMENT FROM ARRAY SETTING TERM
 LOOP WHILE TERM > 2 DO
   DELIM=CHAR(256-TERM)
   REMOVE MORE FROM ARRAY SETTING TERM
   ELEMENT=ELEMENT:DELIM:MORE
 REPEAT
WHILE TERM DO
 . . .
 process
 . . .
REPEAT

The pointer can be reset back to the beginning of the dynamic array by assigning the array to itself:

ARRAY=ARRAY; *Reset REMOVE pointer

Repeated Element Access

Do not access array elements repeatedly. Access once and assign to a simple variable. If accessing values and sub-values, extract the attribute to a simple variable and extract the values and sub-values from this variable.

Building Dynamic Arrays

Concatenation may be considerably faster than using <-1> references when appending to the end of a dynamic array.

Use EQUATE to define the system delimiters used in building a dynamic array:

EQU AM TO CHAR(254), VM TO CHAR(253), SVM TO CHAR(252)
...
ARRAY=ARRAY:AM:VALUE1:VM:VALUE2
...