SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 04/04/2010, 06:18 PM   #1
Zamaroht
Gangsta
 
Zamaroht's Avatar
 
Join Date: Jan 2007
Location: Argentina
Posts: 900
Reputation: 71
Default Timers performance

The original question comes to what is better performance-wise: To have one timer which executes a long function every once in a while, or to redistribute the stuff in different timers with different execution times, so they don't run at the same time.
Most likely, distributing the processing in different timers which run at different intervals would be more efficient, since the load would keep the processor in a more constant processing, instead than hitting it hard every now and then.

But now my question is if this would also apply in extreme cases. For example, which of the following two codes would perform better performance-wise?

pawn Code:
public OnGameModeInit()
{
  SetTimer("LoopFunction", 2500, 1);
  return 1;
}

forward LoopFunction();
public LoopFunction()
{
  for(new i; i < MAX_PLAYERS; i ++) if(IsPlayerConnected(i))
    DoStuffWithPlayer(i);
}

or:
pawn Code:
new Timers[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
  Timers[playerid] = SetTimerEx("DoStuffWithPlayer", 2500, 1, "i", playerid);
  return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
  KillTimer(Timers[playerid]);
  return 1;
}

The second example, would end up running 500 timers at the same time in a full server, and I'm not sure if this would end up being a performance improvement or not against the first example, since it would still need to process the timers' timers.
__________________
Zamaroht is offline   Reply With Quote
Old 04/04/2010, 06:29 PM   #2
dice7
High-roller
 
Join Date: Apr 2009
Location: Slovenia
Posts: 2,200
Reputation: 22
Default Re: Timers performance

The first one
dice7 is offline   Reply With Quote
Old 04/04/2010, 08:05 PM   #3
Zamaroht
Gangsta
 
Zamaroht's Avatar
 
Join Date: Jan 2007
Location: Argentina
Posts: 900
Reputation: 71
Default Re: Timers performance

Quote:
Originally Posted by dice7
The first one
Why is that? Since the second one would perform the same, but in a distributed way.
__________________
Zamaroht is offline   Reply With Quote
Old 04/04/2010, 08:26 PM   #4
RSX
Huge Clucker
 
RSX's Avatar
 
Join Date: Dec 2007
Location: GMT+2
Posts: 338
Reputation: 7
Default Re: Timers performance

checking is fast.. i'd say that i kind'a like first one better, the second one - as you say, end's up with hundreds of timers (and atleast for me - timers tend to glitch ALOT) so maybe the first one is better, only worries about first one comes from, wait, btw IsPlayerNPC could be needed, so that nothing goes... wierd.
So that worries come from possibly huge amount, what makes feeling that last players would get serius lag on this function, but that can be changed if happens, but, in my opinion it would be only when messing strings and floats (coordinate checks).

Finaly i'm saying that i choose first one, keeping it simple !
( Like simple in art, not in newbie scripts (: Look at my crapy avatar, is it simple?)
__________________
HACKED PAWN MEMORY
Log for OXP+ Precompiler :
Done making basic operations... Up next : Finishing basic class {} framework Up next 2 : Making basic rewrite to destination Up next 3 : Making destination options.
RSX is offline   Reply With Quote
Old 04/04/2010, 08:28 PM   #5
ziomal432
Big Clucker
 
Join Date: Dec 2009
Posts: 121
Reputation: 0
Default Re: Timers performance

I bet on first + foreach (written by ******).
ziomal432 is offline   Reply With Quote
Old 30/08/2012, 09:29 AM   #6
ShOoBy
Big Clucker
 
ShOoBy's Avatar
 
Join Date: Mar 2011
Location: nearest dumpster
Posts: 155
Reputation: 60
Default Re: Timers performance

I had this question in my mind 1 year ago , and I basically found out that your first example works better than the second one. I made a test back then, and invited a few friends to help me with it and I found out that the second one also works fine when on the server are less than 10 players, but when there are like 20 or more, it started to lag so now I always use a repetitive timer combined with foreach().
ShOoBy 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
SQL performance Dabombber Help Archive 7 24/09/2009 03:49 PM
[HELP] timers argon Help Archive 8 26/07/2009 06:17 PM
Timers arnutisz Help Archive 0 18/07/2009 06:45 AM
9400 GT - GTA:SA Performance rafay Everything and Nothing 14 09/06/2009 04:23 PM


All times are GMT. The time now is 10:36 PM.


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