• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Zero Second Periodic Timers

Status
Not open for further replies.
Apparently timers with a zero second duration will iterate 10,000+ times a second, while a one with a 0.01 timer will obviously be 100 times a second. Each decimal lower adds another zero.

JASS:
globals
    integer array inc
endglobals

function foo takes nothing returns nothing
    set inc[0] = inc[0] + 1
endfunction

function bar takes nothing returns nothing
    set inc[1] = inc[1] + 1
endfunction

function display takes nothing returns nothing
    call ClearTextMessages()
    call BJDebugMsg("inc[0]=" + I2S(inc[0]))
    call BJDebugMsg("inc[1]=" + I2S(inc[1]))
endfunction

//===========================================================================
function InitTrig takes nothing returns nothing
    call TimerStart(CreateTimer(), 0, true, function foo)
    call TimerStart(CreateTimer(), 0.01, true, function bar)
    call TimerStart(CreateTimer(), 1, true, function display)
endfunction
So a zero second timer is exactly the same as

JASS:
call TimerStart(CreateTimer(), 0.0001, true, function bar)
Nothing special, just maybe someone is curious.
 
I guess that pretty much depends on the threads that are queued.

Basicly, a 0 second timer will create a new thread that is handled as soon as all other currently queued threads are completed. If it takes 3ms to complete all previous tasks, the timer will effectively expire after 3ms (because WC3 is a single-thread application, so the the order of executions is always deterministic). If it takes longer, the timer will take longer, etc.

The reason I'm saying this is that your general conclusion is wrong. On a microscopic scale, time measurements do not have any value, as the time it takes for a thread to execute has a bigger impact than the actual time precision.


It's the same if you create like 500 units. If you run your time measurement directly before and after those units are created, you will have a time difference, even without any timer involved.
 
Ofc tests are welcome, but as far i remember it's indeed the lowest timer timeout ever possible (if you use a value under this 0.0001, included negative values), nothing related to what Zwiebelchen said.
How can you tell it's not related to this? On a microscopic timescale like this, you can never tell for sure.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Well, let's say than a timer(0) will expire as many times as a timer(0.0001).
Timer time was never real anyway, it depends the game speed.
It's true that i didn't used an heavy code inside timer callbacks though.

But i'm quite confident that you will have the very same number of iterations for a Timer(0) with a lightweight code (only a counter) and a Timer(0.0001) with an heavy code inside it (one unit creation/destruction for example) running together.

Now, maybe i'm still missing something you wanted to mean.
 
But i'm quite confident that you will have the very same number of iterations for a Timer(0) with a lightweight code (only a counter) and a Timer(0.0001) with an heavy code inside it (one unit creation/destruction for example) running together.
That would mean that all code gets executed instantly. Unless you invented the quantum computer, that's not possible. ;)
 
Level 14
Joined
Dec 12, 2012
Messages
1,007
Its true that a 0.0 timer expires as often as a 0.0001 timer, but they are still not equal, because a 0.0 timer gets fired before a 0.0001 timer.

Considering the following code:

JASS:
library MyLibrary
	private keyword INITS
	
	struct MyStruct extends array
		private static method callback1 takes nothing returns nothing
			call BJDebugMsg("Timer 1!")
		endmethod
		private static method callback2 takes nothing returns nothing
			call BJDebugMsg("Timer 2!")
		endmethod
		
		implement INITS
	endstruct
	
	private module INITS
		private static method onInit takes nothing returns nothing
			call TimerStart(CreateTimer(), 0.0, false, function thistype.callback1)
			call TimerStart(CreateTimer(), 0.0, false, function thistype.callback2)
		endmethod
	endmodule
endlibrary

Prints:

Timer 1!
Timer 2!

as timer1 was started before timer2. However if we change this now to:

JASS:
library MyLibrary
	private keyword INITS
	
	struct MyStruct extends array
		private static method callback1 takes nothing returns nothing
			call BJDebugMsg("Timer 1!")
		endmethod
		private static method callback2 takes nothing returns nothing
			call BJDebugMsg("Timer 2!")
		endmethod
		
		implement INITS
	endstruct
	
	private module INITS
		private static method onInit takes nothing returns nothing
			call TimerStart(CreateTimer(), 0.0001, false, function thistype.callback1)
			call TimerStart(CreateTimer(), 0.0, false, function thistype.callback2)
		endmethod
	endmodule
endlibrary

It outputs:

Timer 2!
Timer 1!

Therefore a 0.0 timer still has priority over a 0.0001 timer so they are not equal.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
maybe kind of necropost, but this should be dependant also on your CPU power, because if you can run inner game loop 15000 times per second, than 0 second timer will run it for 15000 times per second, but if you can only run it 9876 times per second, then you will obviously run it only at 9876 times per second. You cant run faster than your cpu.

Just a little funny fact:

JASS:
function a takes nothing returns nothing
    call RemoveUnit(CreateUnit(...))
endfunction

function b takes nothing returns nothing
    call TimerStart(CreateTimer(), 0., true, function a)
endfunction

will deadlock the game :D
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
I would say obvious fact, the two heaviest jass operations ever, while limit op is far from to be reached.
And no that wouldn't be dependant from your CPU (unless maybe you mean if you will have some lag or not)

EDIT : Oh i see you took literally what i've said above, try it again but stop the timer after X seconds (but really short, like 0.1 s) or you will probably never see the end.
Or better, try a lighter code, i don't know, something like creating/destroying handles which don't have interact in game, something like a multiboard.
 
Status
Not open for further replies.
Top