PDA

View Full Version : [Tutorial] Player Variables vs Normal Variables.



SiNiSTER
11-09-16, 11:48
Hey, i'm going to start a quick experiment to see what's faster, using normal variables, or PVars. Here's a example of each.

Code:
// Normal.
new
GSystem[MAX_PLAYERS];

Code:
// PVars
SetPVarInt(playerid, "GSystem", 0);

Player variables are new to most scripters. Some prefer it for reasons. This tutorial is split up into several useful information. After talking with my friend krisk about z-Andreas we somehow drifted to a discussion of variables and speed, etc. Quoted from krisk:

Quote:
Originally Posted by IRC
<krisk> PVars are convinient but still variables are twice as fast
I decided actually to check if this is true, by the end of this experiment you will find out of course. We're going to use this code for testing.

Code:
new
string[70],
pname[24];

GetPlayerName(playerid, pname, 24);
format(string, 70, "SELECT * FROM Accounts WHERE Username = '%s'", name);
mysql_query(string); mysql_store_result();
if(mysql_num_rows() > 0) {
// Variable information we're experimenting.
}
else {

}
mysql_free_result();

Speed-O-Krisk

Lets test krisk's opinion, here's what he would do for proof or whatever.

Code:
new
string[70],
pname[24],
bool:HasAccount[MAX_PLAYERS];

GetPlayerName(playerid, pname, 24);
format(string, 70, "SELECT * FROM Accounts WHERE Username = '%s'", name);
mysql_query(string); mysql_store_result();
if(mysql_num_rows() > 0) HasAccount[playerid] = true;
else HasAccount[playerid] = false;
mysql_free_result();

Ok, fine, lets now test the outcome.

Code:
new
string[70],
pname[24],
bool:HasAccount[MAX_PLAYERS],
StartTest, EndTest;

GetPlayerName(playerid, pname, 24);
format(string, 70, "SELECT * FROM Accounts WHERE Username = '%s'", name);
mysql_query(string); mysql_store_result();
StartTest = GetTickCount();
if(mysql_num_rows() > 0) HasAccount[playerid] = true;
else HasAccount[playerid] = false;
EndTest = GetTickCount();
printf("krisk's varable method: %i", EndTest - StartTest);
mysql_free_result();

Speed-O-Carlton

Ok, it's my turn, this is my code I would use.

Code:
new
string[70],
pname[24];
GetPlayerName(playerid, pname, 24);
format(string, 70, "SELECT * FROM Accounts WHERE Username = '%s'", name);
mysql_query(string); mysql_store_result();
if(mysql_num_rows() > 0) SetPVarInt(playerid, "HasAccount", true);
else SetPVarInt(playerid, "HasAccount", false);
mysql_free_result();

Ok, now it's time to test the speed.

Code:
new
string[70],
pname[24],
StartTest, EndTest;

GetPlayerName(playerid, pname, 24);
format(string, 70, "SELECT * FROM Accounts WHERE Username = '%s'", name);
mysql_query(string); mysql_store_result();
StartTest = GetTickCount();
if(mysql_num_rows() > 0) SetPVarInt(playerid, "HasAccount", true);
else SetPVarInt(playerid, "HasAccount", false);
EndTest = GetTickCount();
printf("Carlton's varable method: %i", EndTest - StartTest);
mysql_free_result();


Before the results.

Before the results i'd like to type a little note explaining that SetPVarInt and GetPVarInt calls the function and does what it needs but on the other hand variables does the opposite, it just does what it needs. But then again variables create more cells which means longer compiling.

RAM speed can also do something about the results.

Results #1

Ok, they both came out to be zero. "Oh WTF?". No one ever said variables are faster than a milliseconds even bring to "nanoseconds"!

Quote:
krisk's varable method: 0
Carlton's varable method: 0
Results #2

I tried something different.

Code:
for(new gg = 0; gg < 100000; gg ++ ) SetPVarInt(playerid, "HasAccount", true);
for(new gg = 0; gg < 100000; gg ++ ) HasAccount[playerid] = true;

Results came in.

Quote:
Originally Posted by Server
[22:42:25] Krisk's test took: 14
[22:42:25] Carlton's test took: 40
Oh look we're getting somewhere.

Conclusion

Ok, looks like krisk beat me, and was right, i'll buy him a stack of beer or something. The outcome of this experiment and a summary:
1: Create the code.
2: Get how fast it's without setting it about 100k times
3: Conclude

Normal Variables are faster than player variables!

Thank you for reading this, I hope people who didn't know learned something today, because I did.