• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[General] Global Timer or Many Timers

Status
Not open for further replies.
Level 3
Joined
Mar 31, 2015
Messages
28
I have a question about code efficiency.

If I have some function that does something to unit, and I need to call it repeatedly.

For example, it changes unit's vertex color so that unit turns red, but gradually, so I'd want to call the function 255 times over some time, say two seconds, to change color from (255,255,255) to (255,0,0)).

And suppose I want to do that on many units, possibly at once.

What is better approach here:

(A) to make one global timer which calls the function for every unit in group (when I want the unit to turn red, I add it to the unit group and keep a counter for each unit, timer picks all units, call function, and increase each counter, and whenever unit's counter reach some number (255) I reset it and remove unit from group)

or

(B) make as many timers as there are units to be turned red, and each timer calls function just once (per tick), so there are no unit groups.

Hope the question/example is clear enough.

What is more efficient in terms of performance/memory usage?
 
If I recall correctly, option A is decently more efficient. Just note that most implementations will have a margin of error of [-period to 0] seconds. For example, if you have a timer that ticks every 1 second, and you begin fading a unit, then the "first tick" might expire quicker than 1 second. If you started the fade while the timer was at 0.5 seconds, for example, the first "tick" will occur after 0.5 seconds. All future ticks will occur after 1 second as normal.

In a lot of cases, this isn't a problem--especially with really low periods (e.g. 0.03). But it is something to be aware of.

So I would use option A when you can, and B otherwise. But I usually don't bother with "one-timer" systems that apply to the whole map. Instead, I'll just use one timer for each spell/system, which is kinda a mixture of both A and B. The scheduler works quite well even with a butt-load of timers running simultaneously.
 
Level 7
Joined
Oct 19, 2015
Messages
286
Option A will be more efficient, but you must use a list rather than a group, because looping through the group will be as costly as having a separate timer expire for each unit.

If you're using vJass, you can use something like this module to handle the list and the timer for you.
 
Status
Not open for further replies.
Top