Jump to content

Proper way of incrementing an array's size by 1


Recommended Posts

Correct, and doubling can very quickly hit the limit of 16 *1024*1024 array entries.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

An array with 524288 elements (all empty) would take 3.4 MB of RAM

​That's is not correct AutoIt does not allocate storage for array elements until they actually contain data.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

@Bowmore Try it:

Local $aArray[524288]

MsgBox(0, "Script Paused", "Open Task Manager & Have A Look At Its RAM Usage")

TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

An array with 524288 elements (all empty) would take 3.4 MB of RAM

​The point of doubling the array to add more elements is because you're putting something into the array, they would never be empty until you doubled the array. The first half should always have something in it, otherwise there's no point in expanding the array.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

TD,
until you do not have a rough estimate about how many elements the array will hold at maximum, it is useless to discuss if factor 1.5 or 2 or increasing the size by a fixed number best fits your needs.
Until you do not know how important performance is for the problem you try to solve it is useless to ...

First goes the planning, then the coding!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Correct, and doubling can very quickly hit the limit of 16 *1024*1024 array entries.

​Why? If it hits the limit you filled at least 8.388.608 elements. If you don't use the index 0 as the counter you can even fill the maximum number of elements inside such a array because 16.777.216 is a power of 2. If you increase by 1.5x the maximum value of elements is limited to 11.958.656.

So you only hit the limit if you put a corresponding number of elements. The limit isn't reached faster - you only need lesser ReDims until the limit is reached.

btw.: i made a little UDF which builds on the concept of doubling/halfing the array size if the limit is reached. I putted it as an attachment.

DynArray.zip

Link to comment
Share on other sites

Well, I never intended to start a flame war about this.

But at this rate, why not allocate at once the maximum size head first and finally ReDim it to truncate to the actual used size?

If you don't use the index 0 as the counter you can even fill the maximum number of elements inside such a array because 16.777.216 is a power of 2.

Only if you started at a power of 2.

If you increase by 1.5x the maximum value of elements is limited to 11.958.656.

Why so? Min() can do that.

Anyway, things aren't that simple when the array has more than one dimension.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Well, I never intended to start a flame war about this.

​I also not. It was meant as clarification that the total array size limit  doesn't depend on which increasing factor is chosen.

Why so? Min() can do that.

​Sure - that's why i not understood why you said that doubling will reach the array size faster than with a factor of 1.5x. With both factors you can hold up to 16.777.216 values inside the array. Only the number of redims and the size of the unused space differs - not more. The maximum count of array elements doesn't really influence the choice of the increasing factor. That clarification was my intention here.

Maybe i misunderstood you - maybe you misunderstood me. I never wanted to start a flame war - sorry if it feels like it for you.

Link to comment
Share on other sites

I think 2x is too much and 1.5 times not enough. I reckon 1.61803398875 times to be about perfect. JOKE!

After all is said and done, doubling hits the limit on smaller input. Underestimating an array's size by a factor of 2 can happen, but it's generally an exception. Something like that happens when you need to catalogue a large, and unknown, number of items. In this case it makes sense to pump up the size in larger leaps. Doubling should not be used as a rule of thumb and nobody said it should. I wanted to make this a little clearer.

Link to comment
Share on other sites

The flame war remark was a joke, nothing more.

why you said that doubling will reach the array size faster than with a factor of 1.5x

Because in average use you may want to keep anticipated memory allocation relatively small as a conservative measure and only in rare instances you need to reach a very large number of entries. Expansion factor in many libraries are more in the range 1.2 - 1.5 just to balance number of reallocations and size growth rate. Of course both the initial size and the "optimal" growth factor heavily depend on how well you can estimate the order of magnitude of the final number of entries.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

The flame war remark was a joke, nothing more.

why you said that doubling will reach the array size faster than with a factor of 1.5x

Because in average use you may want to keep anticipated memory allocation relatively small as a conservative measure and only in rare instances you need to reach a very large number of entries. Expansion factor in many libraries are more in the range 1.2 - 1.5 just to balance number of reallocations and size growth rate. Of course both the initial size and the "optimal" growth factor heavily depend on how well you can estimate the order of magnitude of the final number of entries.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I saw that but there is no "delete post" feature. It wasn't to make my point more significant!

There are issues while you have an answer awaiting send and when another post shows up. Not only the post may appear twice but you're unable to go back to where you came from: the history loops on the page.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

It wasn't to make my point more significant!​

:lol:

Because in average use you may want to keep anticipated memory allocation relatively small as a conservative measure and only in rare instances you need to reach a very large number of entries.

Exactly. Now you should understand my question why the maximum number of array elements for a AutoIt array should influence the choise of the growth factor.

I think 2x is too much and 1.5 times not enough.

​I didn't say that 2x or any other factor is the best choice or better than another factor. My point was to scrutinise the argumentation with the maximum array size because it has no real influence. The choice rather depends - as you both already said - on the expectable number of to adding elements and the balancing of avoiding as much redims as possible vs. wasted unused memory amount.

We are in complete agreement - maybe my bad english is the reason for the misunderstandings here - sorry for that.

Link to comment
Share on other sites

Now you should understand my question why the maximum number of array elements for a AutoIt array should influence the choise of the growth factor.

Using a smaller increment allows you to work with larger arrays without going beyond the limit imposed by AutoIt. It's nothing tricky or complicated. :)

Link to comment
Share on other sites

As jchd already mentioned: The factor doesn't influence the maximum number of elements you can store in the array.

Realise the size increasing this way:

ReDim $Array[(UBound($Array) * $factor >= 16777216 ? 16777216 : UBound($Array) * $factor)]

As you can see you will reach the maximum number of 16.777.216 elements always with every growth factor > 1.0.

Link to comment
Share on other sites

@czardas Congratulations on your 7,800th post :)

The best thing to do when you need to ReDim an array is:

Capture2.thumb.PNG.061156a9fae35257eb7ab

 

TD :P

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...