Skip to main content
12-Amethyst
October 31, 2010
Question

Preallocation of Arrays

  • October 31, 2010
  • 2 replies
  • 4937 views

Ref: quicksheets>mathcad techniques>improving calcultion speed.

This artical discusses the importance of preallocation of matrices.

I think I have this right, but just in case I don't:

If I initialize a matrix, say Working and make it a 100x100 matrix, I am in effect assigning a block of memory for this matrix.

I then can use and re-use the Wroking matrix, setting Working=0 before each use, and as long as each use does not exceed the maximum, then the allocation is maintained and being used efficiently. (Different sizes each time).

Is this correct?

2 replies

1-Visitor
October 31, 2010

wayne wrote:

Ref: quicksheets>mathcad techniques>improving calcultion speed.

This artical discusses the importance of preallocation of matrices.

I think I have this right, but just in case I don't:

If I initialize a matrix, say Working and make it a 100x100 matrix, I am in effect assigning a block of memory for this matrix.

I then can use and re-use the Wroking matrix, setting Working=0 before each use, and as long as each use does not exceed the maximum, then the allocation is maintained and being used efficiently. (Different sizes each time).

Is this correct?

This is how I read it.

Calculation speed can be improved when dealing with large matrices or vectors by creating a zero entry at the last index of the array. Which means you are telling the computer how much space should be set aside to fill the array with the calculated values.

It also recommends performing intermediate array operations inside a program. This way the arrays are only created once the program is executed.

There are loads of tricks to speed up calculations.

  • Remove units from program loops
  • Try to work with column vectors
  • Avoid using the stack function
  • Avoid unnecessary vectorization

I'm sure there's more, but can't think of them at the moment.

Mike

1-Visitor
November 3, 2010

Hi

IN a couple of earlier posts, Mike Armstrong explained something that is very important for a user of large arrays in Mathcad. This is not documented anywhere in Mathcad manuals or help areas etc. It should be formally documented by Mathcad so users understand what is happening.

Suppose you do the following in a Mathcad worksheet:

1. create an array TF:= file read from disc, or a matrix you create under programme control in Mathcad etc

2. TF:= TF (manipulated in someway by you under programme control in Mathcad, so an written back into array TF).

3. TF:= TF (manipulated by you in another way under programme control, and so again written back to you into array TF)

Mike explained that within a programme block, temporary files are created and destroyed after leaving the block. But Mathcad needs to hold TF as three separate versions in memory in the above example as otehrwise it cannot compute or process the earlir versions of TF if the memory for Tf is overwritten by a later version of TF. So the memory requirements in the above worksheet example are roughly 3 times the size of TF (depending on what you do for TF at each of the three steps).

A lot of people who programme using other languages try to use memory efficiently and re-use memory if possible. As explained above, this cannot be done inside a Mathcad worksheet.

The end result is if you, like me, input and/or process very large data files, then Mathcad quickly chews through large volumes of RAM and Mathcad will hit a wall and stop working. Switching to full 64-bit operatings system will help in this, but the principle of memory repeats and overflow still remains.

Regards

David

1-Visitor
November 3, 2010

Hi

My apologises, I understand it was Richard Jackson who presented the earlier posts on memory usage.

Regards

David