Raised This Month: $32 Target: $400
 8% 

multiple vs single array handler


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 07-26-2021 , 09:29   multiple vs single array handler
Reply With Quote #1

Hello, suppose I've an item which will contain an unknown number of sub-items. Since the amount of sub-items that the array will have to store is unknown, it's size has to be dynamic. For that I will use cellarray, but I was wondering, which one of the two methods below would be optimal?

Create one array for each item:

Code:
item 1 => ["sub-item 1", "sub-item 1"]

item 2 => ["sub-item 2", "sub-item 2", "sub-item 2"]

item 3 => ["sub-item 3"]

Or store everything in a single array, would look like this:

Code:
["sub-item 1", "sub-item 1", "sub-item 2", "sub-item 2", "sub-item 2", "sub-item 3"]

"item 1" sub-items: 0-1
"item 2" sub-items: 2-4
"item 3" sub-items: 5
__________________








CrazY. is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-26-2021 , 11:30   Re: multiple vs single array handler
Reply With Quote #2

Regarding memory, if you used the second method you would have multiple dynamic arrays then you need multiple internal variables to hold the state of each array, but likely this translates to only a few extra bytes of memory. Of course, few bytes don't matter and this will make absolutely no difference.
Regarding run time complexity, let's say you want to construct a menu for each item and a sub-menu for each sub-item for that item.
1.With the first method, you'd have to iterate over all items which you can do in O(n) assuming n is the number of items and inside that loop, you'd have to loop over all sub-items. Assuming the maximum number of sub-items is m(where m can be anything), you'd have a temporal complexity of O(n * m).
2.With the second method, you will still need to loop over the number of items to add them to the menu. Then, since you know the bounds for each category of sub-items you'd need to iterate over that slice from the array, ending up again with O(n * m) complexity, the same one as the first method. If O() notation is confusing or if you are not familiar with it, what it says is an estimate of how many steps you need to do to run your algorithm. In both cases you need 2 for loops, so the number of steps is the same.
3.To be completely honest, complexity analysis is kinda overkill for most typical scenarios from plugins. You'd likely have like what? 10 items with 10 sub-items each? Even if you have more, certainly you won't end up with millions of items or sub-items.
Even if one method had a better complexity, likely it would make no difference with very few items.

You could think about the CPU cache and how the second method has better spatial locality for the data, especially if all the items fit into a cache line(reducing the delay to read the data from the RAM because it's closer to the CPU), but again this is overkill and it would make sense if you were to continuously access the data from the arrays.

If you were to pick the one that makes your code clear and easier to understand, you'd pick the first option.
If you really wanted to save a few bytes of memory or potentially use the cache better, you'd pick the second one, but realistically I don't see why you care about this.
__________________

Last edited by HamletEagle; 07-26-2021 at 11:37.
HamletEagle is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 07-29-2021 , 21:13   Re: multiple vs single array handler
Reply With Quote #3

Thank you for your explanation. I will probably stick with the first method because it will be easier to manage.
__________________









Last edited by CrazY.; 07-29-2021 at 21:13.
CrazY. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-29-2021 , 21:39   Re: multiple vs single array handler
Reply With Quote #4

Multi-dimension array and size the dimension(s) at the largest potential size and call it a day. We aren't using CPU's from 1990 and have more than 256kb of memory to play with.
__________________

Last edited by Bugsy; 07-29-2021 at 21:40.
Bugsy is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 07-29-2021 , 21:46   Re: multiple vs single array handler
Reply With Quote #5

I currently have an enum with almost 500 of size, planning to increase even more lol
__________________








CrazY. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-29-2021 , 21:48   Re: multiple vs single array handler
Reply With Quote #6

There may be a better way, but no way of knowing based on what you've provided.
__________________
Bugsy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-30-2021 , 01:24   Re: multiple vs single array handler
Reply With Quote #7

Quote:
Originally Posted by CrazY. View Post
I currently have an enum with almost 500 of size, planning to increase even more lol
500 is perfectly fine. Nothing to worry about. Like I said(and so did bugsy) , go with the first method.
__________________

Last edited by HamletEagle; 07-30-2021 at 01:25.
HamletEagle is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 08-02-2021 , 18:53   Re: multiple vs single array handler
Reply With Quote #8

I've something like this, it isn't the 500 sized one. This one is 229, would usually need 10 items of that size, but may vary.

Code:
enum _:ZombieData {     ZombieIdentifier[32],     ZombieName[32],     ZombieInfo[128],     ZombieHealth,     Float:ZombieKnockback,     Float:ZombieSpeed,     Float:ZombieGravity,     ZombiePainshock,     ZombieNVG,     ZombieNVGColor[3],     ZombieShowInMenu,     ZombieLeap,     ZombieLeapForce,     ZombieLeapHeight,     Float:ZombieLeapCooldown,     ZombieFrags,     CvarZombieName,     CvarZombieInfo,     CvarZombieKnockback,     CvarZombieSpeed,     CvarZombieGravity,     CvarZombieNVG,     CvarZombieNVGColor,     CvarZombieLeap,     CvarZombieLeapForce,     CvarZombieLeapHeight,     CvarZombieLeapCooldown,     CvarZombieFrags,     Array:ZombieModels,     Array:ZombieClawModels,     Array:ZombiePainSounds,     Array:ZombieDieSounds,     Array:ZombieFallSounds,     Array:ZombieMissSlashSounds,     Array:ZombieMissWallSounds,     Array:ZombieHitNormalSounds,     Array:ZombieHitStabSounds,     Array:ZombieIdleSounds, }
__________________









Last edited by CrazY.; 08-02-2021 at 18:59.
CrazY. is offline
diegodarknes
Junior Member
Join Date: Aug 2021
Old 08-03-2021 , 22:26   Re: multiple vs single array handler
Reply With Quote #9

hola una pregunta alguiem puede decopilarme estos tres archivos de .sma a .amxx por favor soy nuevo me gustaria que me den unos tipos de tips por favor me alegraria mucho si me lo copilan asi como estan se los agradeceria un monon pero yo lo trate de copilar pero me marcan errores de seguro algunos de ustedes no les salfdran ningun error por favor el me me pueda pasar estos archivos sma a amxx se lo agradeceria un monton
Attached Files
File Type: sma Get Plugin or Get Source (zp_zclass_voodoo.sma - 80 views - 11.0 KB)
File Type: sma Get Plugin or Get Source (zp_zclass_revenant.sma - 69 views - 18.3 KB)
File Type: sma Get Plugin or Get Source (zp_cl_deimosnew.sma - 58 views - 14.1 KB)
diegodarknes is offline
Reply


Thread Tools
Display Modes

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 05:09.


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