Raised This Month: $ Target: $400
 0% 

Arrays (Better coding methods) Expirenced programmers only


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Xanimos
Veteran Member
Join Date: Apr 2005
Location: Florida
Old 04-04-2006 , 17:57   Arrays (Better coding methods) Expirenced programmers only
Reply With Quote #1

[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.
Attached Images
File Type: jpg method-3.jpg (9.4 KB, 181 views)
File Type: jpg method-2.jpg (12.2 KB, 187 views)
File Type: jpg method-1.jpg (9.4 KB, 168 views)
Xanimos is offline
Send a message via AIM to Xanimos Send a message via MSN to Xanimos
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 04-04-2006 , 18:04  
Reply With Quote #2

There is no significant difference between all three; the difference is in how readable your code is.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
Lord of Destruction
Member
Join Date: Sep 2004
Old 04-04-2006 , 18:32  
Reply With Quote #3

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

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 ] );
Lord of Destruction is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 04-04-2006 , 18:40  
Reply With Quote #4

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

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.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
BAILOPAN
Join Date: Jan 2004
Old 04-04-2006 , 19:25  
Reply With Quote #5

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

it's also more readable.
__________________
egg
BAILOPAN is offline
capndurk
Senior Member
Join Date: Feb 2006
Old 04-04-2006 , 19:51  
Reply With Quote #6

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?
capndurk is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 04-04-2006 , 20:01  
Reply With Quote #7

Right, slightly faster. One memory access faster.

And no, it won't take more memory. It takes the exact same amount of memory.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
Xanimos
Veteran Member
Join Date: Apr 2005
Location: Florida
Old 04-04-2006 , 21:25  
Reply With Quote #8

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.
Xanimos is offline
Send a message via AIM to Xanimos Send a message via MSN to Xanimos
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 04-04-2006 , 21:54  
Reply With Quote #9

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
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
capndurk
Senior Member
Join Date: Feb 2006
Old 04-04-2006 , 22:32  
Reply With Quote #10

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.
capndurk 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 16:37.


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