unless they are really big, a 100 000 entry array is likely to be slower to access than a 100 000 entry ds_map
Not sure about reading an array, but any noticeable speed disparity between an array with 100 indexes and one with 100k indexes is due to the 100k array is actually FOUR arrays, not one, and in a lot of cases you are just not handling it properly. I'm not saying you are wrong - I'd need to speed test for myself (and I'm limited to GMS1 now) - but just pointing out when writing to such an array, people typically do it wrong. An array can only technically go up to index 31999. Beyond that, an additional array is created and chained to the other one. Arrays are also created in increments of powers of 2. Couple that with array chaining, you are looking at major slowdown when improperly writing arrays. I don't see how reading an array would be affected, but, again, I'd need to run speed tests and I could see where some bottlenecking could potentially exist.
Just a reminder for experienced users and a lesson for new users:
- Arrays memory use is by powers of 2. An array with 10 indexes uses the same memory as an array with 16 indexes.
- If an array currently uses less memory than the equivalent of an index being written to, the array memory will be expanded to the next power of 2. If an array has 16 indexes and you write to index 16, memory will be set aside for 32 indexes.
- An array can only take up the equivalent memory of 32000 variables. Beyond that, another independent array is created and linked to the previous array, subject to its own rules.
- Memory allocation does not equal initialization. If index initialized, even though the array is allocated the memory for 8 indexes, index 7 is not initialized and thus does not exist.
- Initializing any index will initialize all uninitialized lower indexes inside that array to 0. If an array with up to index 3 already set has index 7 initialized, indexes 4 thru 6 will be set to 0.
- ALWAYS initialize an array using its own highest index. You do not need to fill the array in reverse order, just initialize the highest index (e.g., array[31999]=0).
- Initialize as many indexes as you may need, not the as many as you immediately need. If you only need 20 indexes at the start of the game, but the player may need access to 256 indexes by the end of the game, initialize all 256 indexes at the start.
- Memory expansion for an array is slow. This is the biggest killer of large arrays.
32000 may seem like a lot, but it's not. We can put it into perspective while also analyzing the rules for array write optimization. Let's say you have a grid if data that is a mere 256 cells by 256 cells. Whatever it is intended for, it's just 256x256 - a nice pair of 8bit numbers. You could use a 2D array, but you are cool and hate nested for-loops, so you want to use a 1D array. Do the math and a 256x256 grid has 65536 cells you need to initialize.
If you filled the array with a loop starting at 0 like most beginners, memory would be allocated for 1 variable and set, then increased for 2 variables and set, then increased for 4 variables, then set over two iterations. Then memory would be allocated for 8 variables, then set over 4 iterations. And so forth. Every 32000 indexes, a new array gets created. You test the code and the slowdown is excruciating.
But you're not a beginner and know just setting the highest index will set the rest to 0. So you initialize index 65535. Later, you test your code and when it tries to read index 1, the program crashes and spits out an unknown variable error. Or was it out of range? Whichever it is, it crashed. You debug and find out only indexes 0, 32000, and 64000 & higher were initialized.
You play things safe use a loop to set all 65536 variables in reverse. You test the code and-- There's some noticeable lag once again, but not as bad as before. Why? First memory is allotted for index 66048 -- yes, 66048, not 65535 -- as well as new arrays at 0 and 32000. After the loop writes to 64000, memory is expanded for index 63999. After the loop writes to 32000, more memory is allotted for index 31999, then the loop finishes up.
Pop Quiz: How would you initialize all 65536 indexes with no noticeable slowdown?
The solution is to initialize indexes 31999, 63999, and 65535 separately in that order. After the 3 arrays have been initialized, then you can fill all the indexes in a loop if you want.