Data Access Worldwide Knowledge Base

Article ID 2100
Article Title INFO: Speed in Adding Elements to Dynamic Arrays
Article URL http://www.dataaccess.com/kbasepublic/KBPrint.asp?ArticleID=2100
KBase Category VDF11
Date Created 04/02/2005
Last Edit Date 04/02/2005


Article Text
QUESTION:
In the Help I read:

It is not necessary to use ResizeArray to create new elements of a dynamic array, since Visual DataFlex will create the elements as needed. However,  if you are creating a large array it can be more efficient to create array elements in blocks since adding new members may require the entire array to be moved in memory.

How this "moving the entire array into memory" affect performance?


ANSWER:
Filling up dynamic arrays without first setting its size by moving a sized array to it first, can be extremely slow.  

I monitored the use of memory during a fill-a-large-array loop.  When the array was "rezized" first, the process consumed memory following a linear curve.  When just went
straight to filling it, the increase in memory consumption was following a near parabolic curve and the machine becoming slower and slower.  By increasing the number of elements just slightly I could easily "turn off" the computer (almost irresponsive).

At this stage I killed the process an removed 99% of the fill operations, just filled the *last* element of each row.  This made the CPU fall to almost 0%, but still my computer was almost irresponsive during the process.  The statistics revealed an enourmously heavy memory reallocation task, swapping RAM to disk and back.

I will make this warning:  For even the smallest number of elements that you want to initialize a dynamic array with, as long as you know or can estimate the maximum number of elements, you must always "size" the array before filling it with anything.  Use the ResizeArray function for that:

Move (ResizeArray(MyArray,iMySize)) to MyArray


Everyone who wants to use dynamic arrays should do the test and see the difference on a typical end-user computer.  The effect is almost undetectable or just marginal as long as the total memory consumed is within the limits of the available RAM.

But as soon as it reaches the ceiling, it will start swapping, but not once or twice, in
and out, but it will start a completely new swap in/swap out operation for *each* new value added to the array, and for each new value a heavier and heavier operation is started.

The reason, as you read in the docs, is that the complete array is *moved* into memory for almost each additional element.  This effect is almost like starting the fill operation all over for each new element.



Contributed By:
Knut Sparhell
Company: QA Systems AS
email: knutsp@qasystems.no


Email this Article
Email this Article to a Colleague
Send Feedback on this Article to Data Access Worldwide
Copyright ©2010 Data Access Corporation. All rights reserved.

The information provided in the Data Access Technical Knowledge Base is provided "as is" without warranty of any kind. Data Access Corporation disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Data Access Corporation or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if Data Access Corporation or its suppliers have been advised of the possibility of such damages. Some states do not allow the exclusion or limitation of liability for consequential or incidental damages so the foregoing limitation may not apply.