AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Arrays (Better coding methods) Expirenced programmers only (https://forums.alliedmods.net/showthread.php?t=26564)

Xanimos 04-04-2006 17:57

Arrays (Better coding methods) Expirenced programmers only
 
3 Attachment(s)
[OFF TOPIC]Like title says I don't want people who don't know what their talking about giving dumb answers.[/OFF TOPIC]

[ON TOPIC]

Ok so what I want to know is which method is better for reading and speed.

Say you want to store two or more values per user that have to do with the same thing. What would be a better way of storing it. (Explained in detail below)
1) 2D array with player ID first then split into two or three
2) 2D array split in two or three then into player IDs
3) 2 or more different arrays of just player IDs

Method #1: 2D array Player first (Code then diagram attached)
Code:
// [PLAYER][0] = how many kills // [PALYER][1] = how many deaths new g_PlayerStuff[33][2] //.... console_print(id , "You have %d kills and %d deaths" , g_PlayerStuff[id][0] , g_PlayerStuff[id][1]) //... Or would this be better new kills = g_PlayerStuff[id][0] new deaths = g_PlayerStuff[id][1] console_print(id , "You have %d kills and %d deaths" , kills , deaths)

Method #2: 2D array split first (Code then diagram attached)
Code:
// [0][PLAYER] = how many kills // [1][PLAYER] = how many deaths new g_PlayerStuff[2][33] //.... console_print(id , "You have %d kills and %d deaths" , g_PlayerStuff[0][id] , g_PlayerStuff[1][id]) //... Or would this be better new kills = g_PlayerStuff[0][id] new deaths = g_PlayerStuff[1][id] console_print(id , "You have %d kills and %d deaths" , kills , deaths)
Method #3: 2 arrays (Code then diagram attached)
Code:
new g_PlayerKills[33] new g_PlayerDeaths[33] //... console_print(id , "You have %d kills and %d deaths" , g_PlayerKills[id] , g_PlayerDeaths[id])

Keep in mind that I'm not talking about just saving kills and deaths that was just the first things that came to mind when typing this for an example.

Twilight Suzuka 04-04-2006 18:04

There is no significant difference between all three; the difference is in how readable your code is.

Lord of Destruction 04-04-2006 18:32

how about Method #4: The solution for really experienced coders :roll:

Code:
enum CPlayer {   pKills,   pDeaths,   pName[ 32 ] }; new g_PlayerStuff[ 33 ][ CPlayer ]; // ... console_print(0 , "%s has %d kills and %d deaths", g_PlayerStuff[ id ][ pName ], g_PlayerStuff[ id ][ pKills ],  g_PlayerStuff[ id ][ pDeaths ] );

Twilight Suzuka 04-04-2006 18:40

Quote:

Originally Posted by Lord of Destruction
how about Method #4: The solution for really experienced coders :roll:

Code:
enum CPlayer {   pKills,   pDeaths,   pName[ 32 ] }; new g_PlayerStuff[ 33 ][ CPlayer ]; // ... console_print(0 , "%s has %d kills and %d deaths", g_PlayerStuff[ id ][ pName ], g_PlayerStuff[ id ][ pKills ],  g_PlayerStuff[ id ][ pDeaths ] );

Which is EXACTLY the same as the other methods, except its harder to understand at a glance, and also contains a bit of unnessasary information (the name).

Honestly, for such a simple thing, using an enum is not useful or faster.

BAILOPAN 04-04-2006 19:25

it's technically faster to use two separate arrays as you remove an indirection.

it's also more readable.

capndurk 04-04-2006 19:51

Quote:

Originally Posted by BAILOPAN
it's technically faster to use two separate arrays as you remove an indirection.

it's also more readable.

wouldn't it add size, though, because you use an extra array?

Twilight Suzuka 04-04-2006 20:01

Right, slightly faster. One memory access faster.

And no, it won't take more memory. It takes the exact same amount of memory.

Xanimos 04-04-2006 21:25

Ok, it was just a thought I had today and wanted an answer.

capndurk, I said expirenced programmers only for a reason. I didn't want little scripters' responses.

Twilight Suzuka 04-04-2006 21:54

Quote:

Originally Posted by Suicid3
Ok, it was just a thought I had today and wanted an answer.

capndurk, I said expirenced programmers only for a reason. I didn't want little scripters' responses.

XD

capndurk 04-04-2006 22:32

Quote:

Originally Posted by Suicid3
capndurk, I said expirenced programmers only for a reason. I didn't want little scripters' responses.

Dude, I'm not an inexperienced programmer, I was trying to get an idea of the speed through O notation, and it didn't really seem different:

1 & 2 use the same, and 3 method is 1 less, because it uses an array with 1 less level. And it's not like I was contributing to the discussion, I was just asking a question that I thought needed attention. So sue me for trying to learn.


All times are GMT -4. The time now is 16:37.

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