View Full Version : Timers performance

04/04/2010, 06:18 PM
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?

public OnGameModeInit()
SetTimer("LoopFunction", 2500, 1);
return 1;

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


new Timers[MAX_PLAYERS];

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

public OnPlayerDisconnect(playerid, reason)
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.

04/04/2010, 06:29 PM
The first one

04/04/2010, 08:05 PM
The first one

Why is that? Since the second one would perform the same, but in a distributed way.

04/04/2010, 08:26 PM
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?)

04/04/2010, 08:28 PM
I bet on first + foreach (written by Y_Less).

30/08/2012, 09:29 AM
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().