• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Performance difference between using step-based timers and alarms

T

The Shatner

Guest
Hi there!
I have the habit of using step-based timers, such as:
Code:
CREATE EVENT:
timer = 0;


STEP EVENT:
timer ++;
if (timer >= room_speed) {
         //do stuff
         timer = 0;
}
But GMS/GML has the built-in Alarm System, which does pretty much the same.

Anyone knows if using alarms is more efficient than using the step-based timers?
 
Last edited by a moderator:
N

NeZvers

Guest
There one way to find out - make all alarms on trigger reset time by 1 (making trigger each step) or test in setting longer times and test it with fps_real
Then do the same with variable timers.
Only thing that timers count down and triggers on 0
 

NightFrost

Member
There's also one distinction that might affect which method you want to use: a counter you've written yourself will fire wherever you put it. Alarms on the other hand always fire between Begin Step and Step events.
 
it occurred to me, that you don't need a value incrementing for every timer, you can just use one global time value.

if (global.time >= trigger) {
trigger = global.time + 100000000; //disable timer by setting trigger time to very high value
//do something
}

setting the timer to go off at a certain length of time in the future is just

trigger = global.time + delay
 
M

Marcus12321

Guest
Yes, you could definitely use a global timer, and then set a target time for your timers. But is reading a global variable every step multiple times going to be faster than counting and checking an instance variable? This is something that would again need to be tested. And the truth is, unless you have thousands of them, it probably makes no difference.

One thing you might have to watch out for, if you are counting the micro seconds that delta time returns, is that eventually the number might rollover to a negative number (integer) or be changed to a floating point number (GMS2 controls our variable types, right?) and all of your timers will fail. :) I am not sure when that cut off will be though, but something like a server running 24/7 could eventually find that limit I think.

I am not sure what the likelihood of these things happening is. They might not even be an issue?!?!
 
T

The Shatner

Guest
Well, theoretically, the Alarm is just an instance var (array, actually). If GMS always does a check for alarms anyway, it should be faster to use the alarms since they're already there and already checking. But the performance wouldn't be noticeable unless we had an enormous amount of instances active at a given moment, a technique to be avoided anyway.
It appears that sticking to variable-based timers won't change performance bad enough in order to make the usage of Alarms more suitable. After all, with the var-based timers you can pause, decide when and where they'll fire... It appears less limited than Alarms. Which is also a reason why I suspect that Alarms might be faster.
Oh, but I digress... It appears my original question has more ramifications than I originally thought... How do you guys do your timers? Alarms or vars?
 
B

boob

Guest
Without knowing the exact code behind alarms, which we shouldn't know due to the tenants of encapsulation, we technically can't know which would be more efficient (to my understanding). However, the functionality is such a simple thing that I would imagine GameMaker is already doing the exact same thing as you behind the scenes. Even if there are slight variations, they will functionally have the same O(n).

As far as design goes, you may have more control using your step-based timers. I tend to lean towards using them myself, as there is more you can do with them. However, you are less likely to make mistakes with the built-in alarms, as the timer decrements at a logical pace, outside of your control. There's also the simple matter that your timers count up, while the alarms count down. Which is better, from a design perspective, is really arbitrary and arguments could be made for either side.

TL;DR:
They're probably the same. Which you use comes entirely down to design.
 
Top