Tutorial: variable efficiency
#1

this comes from a forum dedicated to technoligy, programming and designing.

there are 8 other chapters, and its growing like every day. from beginner to advanced,
since i find here most script are most unefficient, and therefore people get bugged or crap servers, i wanted to post chapter 9, the art of efficiency.

anyways, here it is:

9. The art of efficiency
while loads of you programmers/scripters dont think about it. efficiency is vital to a server its performance.

as you might know, servers with a weak internet connection, or simply a weak computer cannot run extreme stuff that takes loads of virtual memory.
efficiency is the answer for this.If you think this is nonsense, i recommend you to pack your bag, and leave the programmers world as it is VITAL to the server computer and/or internet connection.

a server that isn't efficient takes loads of memory, and probably sends a load of information to its clients.Thats why i am giving this tutorial aswell.

lets move on to the codes:
imagine, we have a gamescript, with 20 commands.
all have diffrent uses, but are placed like this: (note: i am using strtok!)
public OnPlayerCommandText(playerid, cmdtext[])
{
new cmd[200], idx;
cmd = strtok(cmdtext, idx);
if(strcmp(cmd, "/register", true) == 0)
{
new tempstring[150];
tempstring = strtok(cmdtext, idx);
//roleplay saving actions, which aren't neccasairy now, i leave them away

SendClientMessage(playerid, PREDEFINED_COLOR, "You succesfully registered!");
return 1;
}
return 1;
}

some of you might think? ehmm so this code is ok?
is it yes, but not efficient. because if you look more closely on it, you see that EVERY TIME a player uses a command, there are at least 2 variables that are made. now imagine, u have a good running server with like 50 players, on every 6 seconds, there is a command used.
i pretty much expect the server to run at least 24 hours. 24 hours = 86400 seconds
86400 / 6 = 14400 . So the same 2 variables, are defined 14400 times in 1 day.
all those 14400 times where unneccasairy. efficiency brings the fix for this.
//note that most likely all programming langauges, it takes the computer less effort to change a value of an excisting variable, then creating a new variable with a new value. note that either way the variable must be set.
//now we are going to globally declare certain variables
new cmd[200], idx;//now, it does not create new variables every command, but, for example idx, it wont work anymore like this, so what to do?

//very simple, we reset the value to its value that it got created with at the top of the used callback, which is zero.


public OnPlayerCommandText(playerid, cmdtext[])
{
idx = 0;
cmd = strtok(cmdtext, idx);
//note that the value assigned to cmd, already changed because of strtok, so we dont need to reset this value.
if(strcmp(cmd, "/register", true) == 0)
{
new tempstring[150];
tempstring = strtok(cmdtext, idx);

//roleplay saving actions, which aren't neccasairy now, i leave them away SendClientMessage(playerid, PREDEFINED_COLOR, "You succesfully registered!");
return 1;
}
return 1;
}


now it doesnt define those 2 variables 14400 times per day.
just resets it. this method is like recycling, you reuse the variables you have used before. making them not go to waste, and save the energy to recreate them.

but now the question, can it be even more efficient?
the answer is yes. plain and simple.
as you can see, the register command uses a new variable already, but we do not have more variables globally defined, and actualy i dont want another one. so what are we going to do?

again, plain and simple. we are going to use the cmd variable, with a littlebit playing with arrays.
i never use cmd as the variable, i use tempstring, since i use the same variables everywhere, in a way they dont interfere, which is efficient, which gives maximum results and performance.

global variables:
new cmd[2][200], idx;//yes, we just added an array on an array, this allows u to use cmd 2 times without overwriting the previous text string (in this case).

public OnPlayerCommandText(playerid, cmdtext[])
{
idx = 0;
cmd[0] = strtok(cmdtext, idx);
//we are using cmd arraysize zero as our command reader, and array size 1 as our temp string
if(strcmp(cmd[0], "/register", true) == 0));
//be carefull here!<dont forget to add arraysize 0 to the variable
{
cmd[2] = strtok(cmdtext, idx);

//roleplay saving actions, which aren't neccasairy now, i leave them away SendClientMessage(playerid, PREDEFINED_COLOR, "You succesfully registered!");
return 1;
}
return 1;
}


so what did we just do?
we replaced more then 14400 times of redefining codes a day with none!
imagine what effort that will be reduced on a 24/7 computer!, just by using this.

efficiency is nothing more then recycling.
you make sure variables do not need to redefined again, IF used alot.
and try to leave the variables count as small as possible by reusing other variables, or adding arrays to excisting ones.

this will greatly increase maximum performance, and your server will be able to handle more information, since there is lesser to process.

note that efficiency doesnt just come from variables, but also the use of code, but thats another story i aint going to explain (YET?!)
Reply
#2

The specified example for server scripts will not correct, since global variables can be used in different threads
Reply
#3

new cmd[2][200];

you wasting ± 80 cells, max input @ onplayercommandtext and @ onplayertext is 128
Reply
#4

Please, don't talk about efficiency if you still use strtok.., and you don't need it in this command.
Reply
#5

i think this topic needs another example to show HOW to make an efficiency code structures for scripts ..
Reply
#6

I don't care how efficient this script is, it won't work with:

pawn Код:
cmd[2] = strtok(cmdtext, idx);
2 -> 1 please

EDIT: and the change between examples 2 and 3 is obvious having already done 1 -> 2, if you're going to make your variables global you may as well do it to all of them in 1 go..
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)