AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   multiple vs single array handler (https://forums.alliedmods.net/showthread.php?t=333633)

CrazY. 07-26-2021 09:29

multiple vs single array handler
 
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


HamletEagle 07-26-2021 11:30

Re: multiple vs single array handler
 
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.

CrazY. 07-29-2021 21:13

Re: multiple vs single array handler
 
Thank you for your explanation. I will probably stick with the first method because it will be easier to manage.

Bugsy 07-29-2021 21:39

Re: multiple vs single array handler
 
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.

CrazY. 07-29-2021 21:46

Re: multiple vs single array handler
 
I currently have an enum with almost 500 of size, planning to increase even more lol

Bugsy 07-29-2021 21:48

Re: multiple vs single array handler
 
There may be a better way, but no way of knowing based on what you've provided.

HamletEagle 07-30-2021 01:24

Re: multiple vs single array handler
 
Quote:

Originally Posted by CrazY. (Post 2754063)
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.

CrazY. 08-02-2021 18:53

Re: multiple vs single array handler
 
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, }

diegodarknes 08-03-2021 22:26

Re: multiple vs single array handler
 
3 Attachment(s)
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


All times are GMT -4. The time now is 02:34.

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