Quote:
|
And I read here that it's not really good for performance specially when it comes with looping, not sure tho.
|
Not sure what you read or where you read it, but this is completely wrong. Looping over a "normal" array or a dynamic array has the same asymptotic complexity of O(array size) because you have to step thru the entire array one cell at a time, there is no way to do it faster. Asymptotic complexity aside, using dynamic arrays is slightly slower because you have to make module calls to ArraySize and ArrayGet*/ArrayPush*, but it doesn't matter.
If you know the maximum number of elements then you use a normal array. If you do not(the number of elements can grow over time with no known upper bound) you use a dynamic array as it can grow in size. This is the only criterion to choose between a normal and a dynamic array. Performance is not a concern.
Dynamic arrays are usually implemented as "normal" arrays, but allocated dynamically on the heap(as opposed to the stack which is used for regular arrays). When the dynamic arrays runs out of space it is simply reallocated with a bigger size.
For example, it is as if you did the following(pseudocode):
PHP Code:
create array of 10 elements
//write 10 elements to array -> array is now full
//try to write another element, the array has no space for it so it grows by doubling its size
resize the array to be able to hold 2 * 10 = 20 elements
write the new element
You can achieve multi-dimensional dynamic arrays, but I don't think you really need that. However, here is a brief explanation(forgive my paint skills):
(there is a smarter way to do this, by liniarising all dimensions into a single array and manually doing the math to compute the real index)
__________________