Posts: 1,219
Threads: 51
Joined: Jul 2012
Hello,
So, i have a
Log script, it offers you to log each players location every X seconds. When a player spawns it sets a timer that will log the location of the player and repeat that until he dies or disconnects. I have been wondering what the differences in memory usage and overall performance are if i would use a global timer that loops through all players.Is anyone able to share his/her knowledge, would be great to know.
And if anyone wonders why i don't test it myself, i can't, because i ain't got no players to test it with D:
greetings Marcel
Posts: 1,679
Threads: 232
Joined: Nov 2009
Reputation:
0
Per player timer will be created at your will which means it will be created at login/register(?) and killed at disconnect(?). A global timer will have to do a check to see if there is any player online and then if he is not dead and if he is registered/logged(?) so it really use any performance data already to make checking.( not really extreme waste but whatever). I may be wrong.
Posts: 1,219
Threads: 51
Joined: Jul 2012
Yeah that is right, but i was wondering how much just the running of the timer matters, let us say there is 1000 player timers running, doesn't that affect the server (ast least the memory consumption??)?
Posts: 1,219
Threads: 51
Joined: Jul 2012
Well, that's exactly why i am worried
Posts: 1,219
Threads: 51
Joined: Jul 2012
I don't really get what you mean, the tiemr doesn't live longer than the server does, ofcause.
And running isn't really a problem, because if there is no players anyways, no one will need the servers power.
But what actually might be an interesting idea, is switching from per player timers to a global timer when a certain amount of online players ahs been reached, so i could balance it.
Posts: 1,219
Threads: 51
Joined: Jul 2012
Pretty easy, no timer needed
PHP Code:
new players = 0;
onConnnect()
{
players++;
}
onDisconnect()
{
players--;
}
anyways, i'd still like to hear a professional opinion, no offense
Posts: 1,219
Threads: 51
Joined: Jul 2012
Quote:
Originally Posted by cessil
pretty sure 99.9% of forum users are amateurs in scripting for sa-mp
performance wise it the different timers won't matter, the only slow down I could foresee is the writing method, but since its every x seconds you should be fine. (I also looked at your github)
you could also do without timers and use OnPlayerUpdate and check the last time you saved position and if it was longer than 2 seconds, save the position.
I fail to see why you're saving positions without a timestamp, if it's to recreate data that has happened then a timestamp would be extremely important.
If it is to recreate someones footsteps for example to see if they are cheating, then you could just save x,y,z,t,v
where xyz is position, t is time and v is vehicle, then I'd run a macro and convert the data to create objects to view in the sa-mp map editor or create 3DTexts to view in game
|
I am saving the timestamp
PHP код:
format(logData, 150, "%s %s's Location X: %f | Y: %f | Z: %f\r\n\n", getDateAndTime(), name, X, Y, Z);
"getDateAndTime()" creates a timestamp
Also, i think onPlayerUpdate is being called very often, esspecially when a player is moving it is called very often
Posts: 1,363
Threads: 14
Joined: Apr 2009
Reputation:
0
I didn't suggest to save every time it's called, reread my post if you don't understand
Posts: 694
Threads: 2
Joined: Oct 2012
Reputation:
0
A timer with foreach would be more optimal for anything below a minute.
Posts: 319
Threads: 7
Joined: Jul 2012
Reputation:
0
Using per-player timers could be a problem when you're hacked.
Hackers are able to call OnPlayerConnect on purpose, making your script to start additional timers.
In the end, your server may be running thousands of timers per playerid and slowly crash your server.
Using a global timer, you don't have that problem.
You start the timer only once and it runs for the entire duration of the server.
You also loop through all players and you check with a simply variable if they logged in properly.
I'm setting that flag when a player logged in and entered the proper password. Only then, the player will be processed in the global timer until they disconnect (OnPlayerDisconnect sets the flag back to false).
That way, players who are still in class selection and haven't entered the proper password for that account will be unable to do anything.
The global timer doesn't process them, and they are blocked from using any command.
If you're worried about performance to do a few variable checks every second (50 if-statements if your server has 50 players), stop programming.
There is other stuff that consumes ALOT more processing power.
Computers these days perform billions of operations per second, so don't worry about 50 boolean checks per second.
They only take a few nanoseconds to perform, which is negligable.
Consider 3D games like GTA.
To calculate each pixel onscreen on a 1024x768 screen (that's 786432 pixels to calculate, 60 times per second).
Don't you think those calculations have at least one if-statement per pixel to be cauculated?
They have more than that and even perform floating point operations which consume more CPU power than an if-statement.
If your CPU would be used 100% doing those calculations (786432 * 60 = 47.185.920 calculations), how much would your CPU be used to perform 50 if-statements (in case an if-statement consume as much power as calculating a pixel)?
50 / 47185920 = about 0.0001 percent CPU power needed to do those checks.
Even for 1000 players: 1000 / 47185920 = about 0.002 percent CPU power.
And it will be alot less because modern CPU's process alot more than this.
So, you see this is nothing.
Don't worry about a timer doing useless checks per player to see if they're logged in or not.
Your CPU won't notice the difference.
You can use foreach to sqeeze off another few nanoseconds but it won't make any difference performance-wise.
I ran a timer that repairs 2000 vehicles on the server (sort of a quick-written GOD-mode for vehicles) every second and it still didn't lag at all.
And that same timer healed every player, still no lag.
And it updated textdraws, calculates vehicle speed, fuel consumption, vehicle health, updates money and score, resets weapons and alot more per player every second.
And that on a normal home-pc on which the game is also running.
Still no lag.