# How to Significantly Reduce Runtime in Cube Voyager by Handling Matrices in Memory

Cube 6.4 has overcome any memory limitation in Cube Voyager*. When dealing with big matrices and reading matrix cells from disk using MATVAL, looping through complex operations can be slow. Using the following tips for handling matrices in memory, runtime can be reduced by 50 percent or more.

*(See the Community for advice on memory usage.)

## Here's how:

To illustrate, we will use a triple indexing application developed by the Western Australia DoT, producing the skim matrix for Park&Ride.

### Original script before improvement

Matrix #1: The original script for the Park&Ride skim matrix used MATVAL to access the matrix cells and had a runtime of 00:02:00 on the computer.

```LOOP K=1, {Zones_Total}
IF (INLIST(K,PNR) = 1)
JLOOP
IF ((MATVAL(1,1,I,K,99999)<>0) && (MATVAL(2,1,K,J,99999) <>0)) ; EXCLUDE NON PNR ZONES
;(....)
; MatVal( filenumber, tablenumber, i, j, failvalue)
MW= (-(WWKMODCH(2,1)+WWKMODCH(2,2)*MATVAL(1,1,I,K,99999)+WWKMODCH(2,3)*MATVAL(1,3,I,K,99999)+
WWKMODCH(2,5)*MATVAL(2,2,K,J,99999)+WWKMODCH(2,6)*MATVAL(2,4,K,J,99999)+
WWKMODCH(2,7)*MATVAL(2,3,K,J,99999)+WWKMODCH(2,8)*MATVAL(3,1,K,J,99999)+
WWKMODCH(2,12)*zonevpl*MATVAL(1,1,I,K,99999))*Cfactor[k]-
WWKMODCH(2,4)*MATVAL(4,1,I,J,99999)-WWKMODCH(2,14)*zonevpw)
IF (MW < MW && MW <> 0)
MW = MW ; PNRTIME
MW = K
ENDIF
ENDIF
ENDJLOOP
ENDIF
ENDLOOP
```

### Improvement step #1: AUTOMDARRAY_1

Matrix #3 uses AUTOMDARRAY in the modified script.

```FILEI MATI = "....\Inputs\MALOO00B_Y2051_target.MAT",
AUTOMDARRAY=MATI1 MI=1-4
```

Calling the input cells values stored in memory as below.

```IF ((MATI1[I][K]<>0) && (MATI2[K][J]<>0)) ; EXCLUDE NON PNR ZONES
;(....)
MW=(-(WWKMODCH(2,1)+WWKMODCH(2,2)*MATI1[I][K]+WWKMODCH(2,3)*MATI1[I][K]+
WWKMODCH(2,5)*MATI2[K][J]+WWKMODCH(2,6)*MATI2[K][J]+
WWKMODCH(2,7)*MATI2[K][J]+WWKMODCH(2,8)*MATI3[K][J]+
WWKMODCH(2,12)*zonevpl*MATI1[I][K])*Cfactor[k]-
WWKMODCH(2,4)*MATI4[I][J]-WWKMODCH(2,14)*zonevpw)
```

Using this step, our example had a runtime of 00:01:22.

### Improvement step #2: AUTOMDARRAY_2

Splitting the process into two steps, in our example, total runtime was reduced to 00:01:06 (circa):

1. Matrix #6 – calculating the K-J skim matrix: Runtime = 00:00:01
2. Matrix #8 – calculating the final I-J PNR AM Skim, using the K-J skim matrix from the previous step, as shown in the script below through MATI5: Runtime = 00:01:05
```IF ((MATI1[I][K]<>0) && (MATI2[K][J]<>0)) ; EXCLUDE NON PNR ZONES
;(....)
MW=(-(WWKMODCH(2,1)+WWKMODCH(2,2)*MATI1[I][K]+WWKMODCH(2,3)*MATI1[I][K]+
MATI5[K][J]+WWKMODCH(2,12)*zonevpl*MATI1[I][K])*MATI5[K][J]-
WWKMODCH(2,4)*MATI4[I][J]-WWKMODCH(2,14)*zonevpw)
```

Therefore, cutting in half the total runtime. Using these tips may provide you with similar or better results.

Do you need more information on how to use AUTOMDARRAY to optimize your process? Our Cube Voyager Reference Guide will provide you with full details about this keyword. And our Citilabs experts are here to help.

Reach out to us if you need a license activation code. And let us know what improvements you are able to gain using these tips. We’d like to hear from you! 