Raised This Month: $ Target: $400
 0% 

3 Dimensional array using ArrayCreate()?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 12-30-2021 , 06:54   Re: 3 Dimensional array using ArrayCreate()?
Reply With Quote #1

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)
__________________

Last edited by HamletEagle; 12-30-2021 at 15:21.
HamletEagle is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:32.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode