Raised This Month: $ Target: $400
 0% 

Array VS Multiple Array


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-19-2021 , 14:28   Re: Array VS Multiple Array
Reply With Quote #9

I agree that it doesn't matter. However, I want to point out a situation in which it having the data together vs splitting the data would matter.

If dynamic arrays were implemented under the hood using a structure like linked lists then it would make a difference. Accessing an index into an array, like array[i] is always an O(1) operation, meaning it doesn't matter if you access the first or the last index, it takes the same number of steps(or if it helps you better visualize this, "the same time", even if not technically correct).
In a linked list, to access the i-th index you have to loop thru all the other i-1 elements to get to the i-th element. This means that accessing the first element is only one step and accessing the last element takes a number of steps equal to the length of the list.

Let's say we split the data in 3 parts:

With arrays, we would have something like:
Code:
array1[i] <- one step
array2[i] <- one step
array3[i] <- one step
no matter which "i" we access, even if i = 1000000 we still perform one step
In total we are doing 3 steps, which isn't very different than doing one step.

With a list, assuming we have a long list and assuming we want to access the element with the index = 1 million

Code:
list1[1000000] <- 1000000 steps
list2[1000000] <- 1000000 steps
list3[1000000] <- 1000000 steps
In total, we get 3 million steps. On the other hand, if you had only one list you'd do only 1 million steps. 3 million steps vs 1 million steps is a pretty big difference.

Some of you may be quick to point out that asymptotically O(3*n) = O(n) and you'd be right, but for a large "n" the constant 3 actually matters and can cause a very noticeable slowdown. For small "n" it wouldn't make a difference.

You can code just fine without understanding the above explanation, especially because you don't work with huge amounts of data in plugins and we don't have list like structures, but I figured someone could find this useful.
__________________

Last edited by HamletEagle; 06-19-2021 at 14:30.
HamletEagle is offline
 



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 02:31.


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