SA-MP Forums Archive
Which is better to do? enum in memory or a variable every time? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Which is better to do? enum in memory or a variable every time? (/showthread.php?tid=632532)



Which is better to do? enum in memory or a variable every time? - iSanchez - 15.04.2017

Hello!

Is better to do, this:

PHP код:
enum ePlayerData{
accountid,
score,
money,
IP[17],
SERIAL[42],
deaths
}
new 
PlayerData[MAX_PLAYERS][ePlayerData];
stock StorePlayerSerial(iPlayerID)
{
    new 
        
szSerial[41]; // 40 + \0
 
    
gpci(iPlayerIDszSerialsizeof(szSerial));
    
memcpy(PlayerData[iPlayerID][SERIAL], szSerial0strlen(szSerial) * 442);
    return 
szSerial;
}
//here it's not possible to return PlayerInfo[playerid][IP] because it will return a wrong value, I know. However I can use the PlayerInfo[playerid][IP] variable in the whole gamemode without problems, I have tested it.
stock StorePlayerIP(playerid)
{
    
GetPlayerIp(playeridPlayerInfo[playerid][IP], 17);
    return 
1;

So I can use the IP and SERIAL variables later at any time using
PHP код:
PlayerData[playerid][IP
or this way:
PHP код:
stock GetPlayerIP(playerid)
{
       new  
ipaddr[17];
       
GetPlayerIp(playeridipaddrsizeof(ipaddr));
       return 
ipaddr;
}
stock GetPlayerSerial(iPlayerID)
{
    new  
szSerial[42];
    
gpci(iPlayerIDszSerialsizeof(szSerial));
    return 
szSerial;

I think the second way uses more memory because it creaste a string of 42 bytes (serial) everytime, but I'm not sure.

Meaning of ram memory/cpu/etc.. which is better to use and why?

Thank you.


Re: Which is better to do? enum in memory or a variable every time? - DarkSkull - 15.04.2017

Storing it in a variable when the player connects is better, Because You don't have to call a function everytime you need a players IP/Serial.


Respuesta: Re: Which is better to do? enum in memory or a variable every time? - iSanchez - 15.04.2017

Quote:
Originally Posted by DarkSkull
Посмотреть сообщение
Storing it in a variable when the player connects is better, Because You don't have to call a function everytime you need a players IP/Serial.
So, everytime I call the function in the second way, it wastes +42 bytes in memory or.. what :0?
Is the memory never "flushed" using the second way?


Re: Respuesta: Re: Which is better to do? enum in memory or a variable every time? - DarkSkull - 15.04.2017

Quote:
Originally Posted by iSanchez
Посмотреть сообщение
So, everytime I call the function in the second way, it wastes +42 bytes in memory or.. what :0?
Is the memory never "flushed" using the second way?
It don't know if it wastes memory, But it gets the players IP again and again, which is slower that just using the function once and storing it in a variable.


Re: Which is better to do? enum in memory or a variable every time? - Vince - 15.04.2017

Local variables are automatically discarded as soon as control leaves the function. Also a cell is actually 4 bytes; 32 bits with 8 bits per byte.

I was never a fan of those wrapper functions. People that use those tend to mindlessly scatter them around; often calling the wrapper multiple times within the same function where it would have obviously been better to get the value once. I personally tend to store the non-changing information (name, ip, computer id) globally upon connect and then I reference it from there.


Respuesta: Re: Which is better to do? enum in memory or a variable every time? - iSanchez - 15.04.2017

Quote:
Originally Posted by Vince
Посмотреть сообщение
Local variables are automatically discarded as soon as control leaves the function. Also a cell is actually 4 bytes; 32 bits with 8 bits per byte.

I was never a fan of those wrapper functions. People that use those tend to mindlessly scatter them around; often calling the wrapper multiple times within the same function where it would have obviously been better to get the value once. I personally tend to store the non-changing information (name, ip, computer id) globally upon connect and then I reference it from there.
nice explanation Thanks again.

Edit.

I didn't understand well this:

Quote:
Originally Posted by Vince
Посмотреть сообщение
Also a cell is actually 4 bytes; 32 bits with 8 bits per byte.
That means what if I create a variable with 256 cells, it will have 1024 bytes (because of 256 * 4 is 1024 )?

If yes, what does "32 bits with 8 bits" means?


Re: Which is better to do? enum in memory or a variable every time? - RIDE2DAY - 15.04.2017

A variable has 32 bits.
PHP код:
new g_Var// 32 bits = 4 bytes 
If 32 bits are 4 bytes, then each byte has 8 bits. Right? Just as simple as 32/4 = 8.

If you declare 256 cells, you have 256 "variables", then: 256 * 4 bytes = 1024 bytes = 1.024 kylobytes = 0.001024 megabytes