Dynamic Arrays performance?

Previous topic - Next topic

Dark Schneider

For dynamic arrays we use:

Code (glbasic) Select

LOCAL MyArray[]
DIM MyArray[0]
...
DIMPUSH MyArray[], data


All we know the cost of allocate memory in runtime, maybe there is the only way (until you want to make your own List) but, could something like this helps?:

Code (glbasic) Select

LOCAL MyArray[]
DIM MyArray[1024]
REDIM MyArray[0]
...
DIMPUSH MyArray[], data


I mean, some languages have the option to create collections with a base size (maybe C#, I don't remember well  :zzz: ) and then when you overpass that size it begins with the dynamic growing.

Slydog

That would be really useful, especially in game programming where every ms counts.
And of course BOUNDS() would return the size of the data added, not the size of the potential buffer (1024 in this case).

And you could extend on this by adapting the command to accept a 2nd parameter:
DIM MyArray[1024, 128]
that would tell it how much to expand the array when each time it reaches its limit so it won't then just expand by one every time causing the original issue.
Maybe another command would be needed (RESERVE MyArray[1024,128] or something)
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Moru

You you could just do that yourself, it's easy enough :-)

My projects needing big arrays that only grows is usually created like this:

When adding something to list:
Check size of array, if size smaller than write-pointer, add 10000 entries (or whatever you like)
Insert new data at end
Increase write-pointer

If I have an array that needs to also delete items I use a field called "active" in a type-array. A loop goes thru the array from beginning or from the pointer "last_deleted" and adds data to this location. if nothing found, it creates size like above. Usually I never allow an array to shrink since these projects are only created for working with data, not gaming. If you want to shrink such an array you need to recreate it in a temp-array as if you were defragmenting a harddrive I guess.