General Question About Timers
#1

Hello,

Regarding the usage of timers in scripts - I understand that using a lot of them is bad but does anybody have any data regarding differences in use, number of timers etc.

I'd be very interested in seeing it if anybody does. I will try and gather my own data, too.

I came across this post while doing some research (The thread was about high CPU usage):
Quote:
Originally Posted by Maki187
Посмотреть сообщение
That usualy happen when you forgot to put return 1; on the ond of timer or onplayerupdate, or you jsut have too many timers per player.
Can anybody back him up on the "more CPU usage when 'return 1;' is left out of timers"?

Regards,

JonRB
Reply
#2

It doesn't matter if you have return or not the default return is always 0 even if return is not present.

pawn Код:
#include <a_samp>

public OnFilterScriptInit()
{
    new a = 4;
    printf("Value before function: %i", a);
    a = TestFunction();
    printf("Value after function: %i", a);
    return 1;
}

TestFunction()
{
}
This will print 4 and 0
Reply
#3

Timers, in general, are very CPU hungry. However, if you have a player-base of 25 or below, it shouldn't be too bad.
Reply
#4

I understand that, but the only possibility that I could come up with is that (Like some callbacks) a timer's behaviour might be influenced by return values. (Obviously timers don't traverse through scripts like callbacks do, though, so it's probably untrue, but I haven't been able to get enough information either from trying things or checking PAWN documentation).


Quote:
Originally Posted by iKyle
Посмотреть сообщение
Timers, in general, are very CPU hungry. However, if you have a player-base of 25 or below, it shouldn't be too bad.
This is the kind of statement I mean. I'm yet to find anything to substantiate this.

--------------

EDIT: I've started some preliminary testing and the numbers are suggesting that having fewer timers with the same amount of work is indeed better (at least memory wise)

Here you can see the difference between 'many timers' (top server) and 'single timer' (bottom server).



The amount of 'work' being done by each server is (Theoretically) the same:

- The many-timer server starts 200,000 timers (I don't know if they all actually call - I doubt it a little), each timer call causes 1 unit of work to be done.
- The single-timer server starts 1 timer, which does 200,000 units of work.

Admittedly on your 'average SA-MP server', the difference will be a lot less significant (I hope!) as in this example a LOT of timers are being run. These results obviously make a fair bit of sense because the SA-MP server has to keep time for a lot of separate timers.

--------------

On a marginally more amusing note, I'm proud to announce that if you spawn 50m timers the server will use over 1GB of memory - My computer is not happy with me:
Reply
#5

I'd like to bump this, if I may. Is there anybody out there with a good idea of how to guage the effects of timers on CPU usage?
Reply
#6

Quote:
Originally Posted by jonrb
Посмотреть сообщение
I'd like to bump this, if I may. Is there anybody out there with a good idea of how to guage the effects of timers on CPU usage?
The only thing that would affect timers is if the function that the timer is calling is really un-optimized.

When i was new to scripting people always told me the same misconception that timers are bad, they cause high cpu issues, use as few timers as possible, and i had tons of code that was extremely slow under one timer. And wouldn't you know, my server used a decent amount of cpu usage per player cause it was running everything at once. I then used more timers splitting things up, and guess what, my cpu issues were gone and the server even had better sync and less issues with timeouts.

So from my own experience, the entire misconception about timers is quite wrong. Watch what your calling with each timer and make sure the code that is being executed by each timer isn't massive loops inside more loops that have another while loop......you should know where i'm trying to get at. The same goes for onplayerupdate.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)