Performance Question , Per Player Timer vs Timer that loops through all players
#1

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
Reply
#2

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.
Reply
#3

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??)?
Reply
#4

Quote:
Originally Posted by [Bios]Marcel
View Post
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??)?
It doesn't really depends on how many timers you have but how often you use it I mean if you have 1000 timers that are used every 1 second then good luck.BOOM.
Reply
#5

Well, that's exactly why i am worried
Reply
#6

Quote:
Originally Posted by [Bios]Marcel
View Post
Well, that's exactly why i am worried
Also do not forget that if you make a global timer it MUST be killed when server closes until next restart. So imagine already how much damage you can done with one global timer that checks every 2 seconds without any reason(no player online?/everyone is dead?/no one registered-logged?). At least per player will be killed everytime player disconnects so its more efficient.
Reply
#7

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.
Reply
#8

Quote:
Originally Posted by [Bios]Marcel
View Post
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.
Tell me how you will check if any player is online to use a global timer? YOU WILL NEED ANOTHER TIMER ALREADY or use the same one to check is anyone is online. So both ways you will use timer for no reason if there is no player online.
Reply
#9

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
Reply
#10

Quote:
Originally Posted by [Bios]Marcel
View Post
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
That is a very amateur way. Anyway, since you need a professional answer. Your answer is Per player Timer.
Global timer will take time to execute FOR ALL PLAYERS and it night cause sync problem. Per player timers are smaller chopped timers so it will reduce sync problems. However, there are more posts like yours on forums and you should check them
Apologizing for writing in a hurry but I am from phone.
Hope I helped cheers.
Reply
#11

Quote:
Originally Posted by [Bios]Marcel
Посмотреть сообщение
anyways, i'd still like to hear a professional opinion, no offense
Quote:
Originally Posted by vassilis
Посмотреть сообщение
That is a very amateur way. Anyway, since you need a professional answer. Your answer is Per player Timer.
Global timer will take time to execute FOR ALL PLAYERS and it night cause sync problem. Per player timers are smaller chopped timers so it will reduce sync problems. However, there are more posts like yours on forums and you should check them
Apologizing for writing in a hurry but I am from phone.
Hope I helped cheers.
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
Reply
#12

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(logData150"%s %s's Location X: %f | Y: %f | Z: %f\r\n\n"getDateAndTime(), nameXYZ); 
"getDateAndTime()" creates a timestamp

Also, i think onPlayerUpdate is being called very often, esspecially when a player is moving it is called very often
Reply
#13

I didn't suggest to save every time it's called, reread my post if you don't understand
Reply
#14

A timer with foreach would be more optimal for anything below a minute.
Reply
#15

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)