SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 11/12/2016, 09:14 AM   #11
cessil
High-roller
 
cessil's Avatar
 
Join Date: Apr 2009
Posts: 1,346
Reputation: 296
Default Re: Performance Question , Per Player Timer vs Timer that loops through all players

Quote:
Originally Posted by [Bios]Marcel View Post
anyways, i'd still like to hear a professional opinion, no offense
Quote:
Originally Posted by vassilis View Post
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
cessil is offline   Reply With Quote
Old 11/12/2016, 10:47 AM   #12
BiosMarcel
Banned
 
Join Date: Jul 2012
Location: Germany
Posts: 1,219
Reputation: 234
Default Re: Performance Question , Per Player Timer vs Timer that loops through all players

Quote:
Originally Posted by cessil View Post
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 Code:
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
BiosMarcel is offline   Reply With Quote
Old 12/12/2016, 01:28 AM   #13
cessil
High-roller
 
cessil's Avatar
 
Join Date: Apr 2009
Posts: 1,346
Reputation: 296
Default Re: Performance Question , Per Player Timer vs Timer that loops through all players

I didn't suggest to save every time it's called, reread my post if you don't understand
cessil is offline   Reply With Quote
Old 12/12/2016, 07:36 AM   #14
PrO.GameR
Gangsta
 
PrO.GameR's Avatar
 
Join Date: Oct 2012
Posts: 694
Reputation: 121
Default Re: Performance Question , Per Player Timer vs Timer that loops through all players

A timer with foreach would be more optimal for anything below a minute.
__________________
Blueberry Prison Roleplay will be back soon!
Follow the forums for more information about opening day.

Forums
PrO.GameR is offline   Reply With Quote
Old 12/12/2016, 09:50 AM   #15
AmigaBlizzard
Huge Clucker
 
Join Date: Jul 2012
Posts: 319
Reputation: 60
Default Re: Performance Question , Per Player Timer vs Timer that loops through all players

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.
AmigaBlizzard is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which is better Per Player Timer or Global timer? (issue with timer) Dokins Scripting Help 4 07/06/2016 09:50 AM
Question about timer for a player. Stoyanov Scripting Help 2 23/09/2015 07:26 PM
Timer i all players to one player? Admigo Scripting Help 7 25/08/2012 10:03 PM
timer for all players? Evation Scripting Help 4 30/05/2012 03:01 AM
A timer that loops through all players or a timer for each player? x-cutter Help Archive 2 28/05/2011 03:35 PM


All times are GMT. The time now is 05:07 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.