SA-MP Forums Archive
Multiple variables vs one variable speed performance? - 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: Multiple variables vs one variable speed performance? (/showthread.php?tid=651655)



Multiple variables vs one variable speed performance? - ElMaestro123 - 24.03.2018

Hello. I have a question about best anticheat checks that are placed inside foreach loops. I need precise and correct answer for usage of variables, since I don't know is it better to use one variable created on top of timer of anticheat and then used for each player to check if they are cheating or not. So the variable's value will be changed ~300 times in loop and loop needs to be finished asap efficiently. I've seen that I've created a stock function where the new variable is being declared in that stock.

CODE:

Код:
stock Checksomething( playerid ) {
new x;
// using x here
}

public anticheat() {
    foreach ( new i : Player ) {
         Checksomething( i );

    }

}
and I've replaced it to:

Код:
public anticheat() {
    new x;
    foreach ( new i : Player ) {
          // CODE USING x VARIABLE THAT'S DECLARED IN THIS TIMER, NOT IN FUNCTION.
    }
}
Which code is more efficient and why?


Re: Multiple variables vs one variable speed performance? - v1k1nG - 24.03.2018

Oh well how do you use x?


Re: Multiple variables vs one variable speed performance? - ElMaestro123 - 24.03.2018

I just mentioned x as an example, although x will have it's value changed throughout the loop many times since the loop will be performed on over 200 online players.


Re: Multiple variables vs one variable speed performance? - v1k1nG - 24.03.2018

Ok then I bet my money on the stock then.


Re: Multiple variables vs one variable speed performance? - Grim_ - 25.03.2018

There would be no difference that is noticeable. Technically, calling another function would add more execution time, but it would be so miniscule you would never notice (seriously).

Create your script, test it and profile it, THEN, if you need to improve the performance, focus on the calls with the highest execution time.