SA-MP Forums Archive
SetTimerEx or SetTimer+foreach ? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: SetTimerEx or SetTimer+foreach ? (/showthread.php?tid=429731)



SetTimerEx or SetTimer+foreach ? - iJumbo - 10.04.2013

hey ya!

Ok i have a question, a performance question.


Its better use

(all codes here its just examples)

pawn Код:
public OnPlayerConnect(playerid)
{
    playertimer[playerid] = SetTimerEx("GiveCookie",500,true,"i",playerid);
    return 1;
}

public GiveCookie(playerid)
{
    CookieStatus[playerid][cookieamount]++;
}
Or

pawn Код:
public OnGameModeInit()
{
    SetTimerEx("GiveCookie",500,true);
    return 1;
}

public GiveCookie()
{
    foreach(new i : Player) {
        CookieStatus[i][cookieamount]++;
    }
}

?


Re: SetTimerEx or SetTimer+foreach ? - CaRa - 11.04.2013

In my opinion using foreach is more efficient than SetTimerEx .


Re: SetTimerEx or SetTimer+foreach ? - iJumbo - 11.04.2013

foreach too with large amount of var set? or large checks?


Re: SetTimerEx or SetTimer+foreach ? - CaRa - 11.04.2013

I don't understand what you meant to say ...


Re: SetTimerEx or SetTimer+foreach ? - thefatshizms - 11.04.2013

I have no clue, but I would say the foreach as that's just one timer and not 500 (or how many people on the server) timers.

But as I said, I don't really know.


Re: SetTimerEx or SetTimer+foreach ? - iJumbo - 11.04.2013

Using the foreach case and not setting just a cookie var but like 70 vars?


Re: SetTimerEx or SetTimer+foreach ? - Pottus - 11.04.2013

I'd go with the second option it's better suited since you can leave out the overhead of Setting and Killing timers.


Re: SetTimerEx or SetTimer+foreach ? - iJumbo - 11.04.2013

So the second option is better in performance?


Re: SetTimerEx or SetTimer+foreach ? - MP2 - 11.04.2013

I don't know exactly how timers in SA-MP work, but here's a likely theory:

In the server's main 'timer', all timers are looped through to see whether they are needed to be called. This means, if you have a lot of timers, it's looping through a LOT more timers to check whether they need to be called. It's also possible that expired timers are also checked, which means using a timer for every player is likely less efficient.

I think ****** said something regarding this once.


Re: SetTimerEx or SetTimer+foreach ? - Pottus - 11.04.2013

Quote:
Originally Posted by iJumbo
Посмотреть сообщение
So the second option is better in performance?
Definitely, no need to SetTimer on connect or KillTimer on disconnect which saves some function calls but I think what MP2 has to say is most likely along the lines of why lots of timers are poor performance wise.