HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Question about timer efficincy algorithm

12-14-2008, 09:43 PM#1
Flame_Phoenix
Hi guys, I have a spell called DarkLightning and recently ANitarf suggested me to use a single timer. The problem with my spell is that it creates a timer every time some one casts an instance of it (this is drastically dynamic), however, if I only use one timer, I will always have 1 timer running (even if paused) thus consuming precious resources.

This way decided to come up with an idea which I explain in detail now:
- When some one casts an instance of the spell for the first time I use "NewTimer()" (from TimerUtils) and create a single timer that will live for 20 seconds. If during those 20 seconds no one casts the spell again I release the timer so other things can use it. If, on the other hand, people keep casting the spell, the life time of the timer will be reseted each time a spell is cast, thus always extending the life of the timer.

This solution works for both ways, it only has 1 timer, and if the timer is not needed than it is released.

I intend to do this by using a function or a method with a real counter or something similar (I didn't implemented it yet).

My question is about this control function or variable, will it ruin the efficiency of the spell ? Will it be to heavy ?

I need the opinion of an expert if possible.

Thx for you time, Flame_Phoenix.

PS: for those who are curious, here is the link for the spell:
http://www.wc3campaigns.net/showthread.php?t=102706
12-14-2008, 10:11 PM#2
Zerzax
Use a global Flame Phoenix, it is an extremely efficient way of doing it.

Alright I'll amend what I said: Take a look at this: http://wc3campaigns.net/showthread.php?t=100953
It loops through all currently active struct instances, and it's the best way I've seen yet.
12-14-2008, 10:29 PM#3
moyack
Have you read my tutorial about how to develop spells over time?? Check my signature.

Using a single timer doesn't consume impressive amounts or resources as you thought, but I think the target audience is actually the problem.

Let's see the possibilities: let's say that we want to apply your spell to normal units, so if we have a footies map in a full house, and all the players has accumulated 60 - 100 units per player, then we'll have as maximum 1200 units, now let's say that the spell is casted by all the units...at the same time... 1200 timers are created, used, and when the spell is ended the timers remains there, created, and ready to be used again, but "consuming precious resources"... by 1200 times. So the approach of a timer per cast is not so reasonable in this case, a single timer controlling everything sounds definitely the best and the obvious.

But what if we plan to use your spell with heroes... then one timer that runs, for too few units, is practically excessive. Creating and recycling 12 timers as maximum is practical and logical, allowing you to have more control in the effect dealt by the spell.

Now let's talk about efficiency... multiple timers implies multiple threads running in parallel a spell code, in that way it's efficient. One timer ruling all the instances of a spell, could become laggy, but it depends of how effective is the code run for each instance. Ejem... well, if the looping code is bad done in the multiple timers approach, the lag will be amplified too... so the issue could be a real problem according in how well is coded the looping function (AKA is up to the coder).

About your solution, I think it's not the best, even more is overcomplicated. To be honest, you're exaggerating the problem, having one timer running forever don't kill a game, what could kill it is a bad coded looping function.
12-14-2008, 10:33 PM#4
Zerzax
Considering the nature of your spell and that it's likely a hero spell, you can probably use TimerUtils normally. Good explanation Moyack.
12-15-2008, 03:29 AM#5
chobibo
T vs N, where N=a*b, a is the number of all spells with dynamic timers, and b being the number of units that will be using timers, T=c, c is the number of spells using a single timer. Actually I'm just repeating what moyack said, if you're worried about multiple timers idling then why not use TT, or if you want to, use a single timer for all spells, if that is even possible.

Assuming that there are 5 spells in need of use of timers and 60 active units, they all cast all 5 spells.
Code:
N's algorithm:
a = 5; b = 60;

N = a * b;
N = 5 * 60
M = 300
You'll need a maximum of 300 timers to handle possible timer needs, if all units cast all 5 spells simultaneously.

Code:
Now in T's case, where a single timer controls each spell
T=c;
T=5
You'll need a maximum of 5 timers for all, even if all active units cast all those five spells simultaneously.

T vs N
300 vs 5

So, in conclusion, the dynamic timer usage grows with the population of the map relative to the number of spells.

EDIT: TimerUtils doesn't destroy a timer, it doesn't free memory, it let's it idle, stored in memory, it places the timers in a stack, whenever you need a timer it takes a timer from the stack, if there is one, else it creates a new one.

What you're trying to do is get a piece of paper from the trash can and throw it back again where your got it, the trash can.

Your solution would be void and useless since TimerUtils already does what you want to do with your timer.
12-15-2008, 03:37 AM#6
PandaMine
As moyack said, its depends on the code that is being iterated through the loop.

There is a limit of 200000 bytes for every thread execution in JASS, so if you're code takes 100 bytes every loop, then it theoritically handle up to 2000 units (quite a bit). If you still want to use a single timer (and you are forced to because there are going to be a lot of units using the spell) AND you have made the code in you're loop as effecient as possible, you may wan't to consider creating a seperate thread in you're loop and putting you're code in there.
12-15-2008, 07:59 PM#7
Flame_Phoenix
Ok guys, thx for your opinions I shall now move on with the spell =)

+rep for all !