Page 1 of 1

A more precise sleep()

Posted: 21 Jan 2024, 08:57
by Phantom
Sub-millisecond sleep support started after win-10 version 1803.

This is the result of my testing:
1 0.001020
2 0.002012
3 0.003022
4 0.004028
5 0.005412
6 0.006024
7 0.007371
8 0.008039
9 0.009366
10 0.010514
11 0.011150
12 0.012660
13 0.013447
14 0.014487
15 0.015591
16 0.016617
17 0.017301
18 0.018379
19 0.019212
20 0.020619
21 0.021420
22 0.022159
23 0.023147
24 0.024528
25 0.025076
26 0.026495
27 0.027124
28 0.028146
29 0.029166
30 0.030034
31 0.031186
32 0.032491
33 0.033499
34 0.034575
35 0.035232
36 0.036287
37 0.037404
38 0.038395
39 0.039829
40 0.040187
41 0.041769
42 0.042465
43 0.043688
44 0.044529
45 0.045268
46 0.046269
47 0.047393
48 0.048645
49 0.049588
50 0.050129

Code: Select all

st := ""
loop 50
{
	DllCall('QueryPerformanceCounter', 'Int64P', &g_time1 := 0)
	sleep2(A_Index)
	DllCall('QueryPerformanceCounter', 'Int64P', &g_time2 := 0)
	st .= A_Index "`t" Round((g_time2 - g_time1) / 10000000, 6) "`n"
}

MsgBox st

sleep2(ti) {
	if (ht := DllCall("CreateWaitableTimerExW", "ptr", 0, "ptr", 0, "uint", 3, "uint", 0x1F0003, "uptr"))
		&& DllCall("SetWaitableTimer", "uptr", ht, "uint64*", ti * -10000, "int", 0, "ptr", 0, "ptr", 0, "int", 0)
		DllCall("WaitForSingleObject", "uptr", ht, "UInt", 0xFFFFFFFF), DllCall('CloseHandle', "uptr", ht)
}

Re: A more precise sleep()

Posted: 21 Jan 2024, 10:58
by vmech
Your math is wrong. Smallest time interval you shown is 1 millisecond.

Re: A more precise sleep()

Posted: 21 Jan 2024, 20:41
by Phantom
vmech wrote:
21 Jan 2024, 10:58
Your math is wrong. Smallest time interval you shown is 1 millisecond.
CreateWaitableTimerExW() The accuracy of the CREATE_WAITABLE_TIMER_HIGH_RESOLUTION option is 1 ms.
Less than 1 millisecond becomes inaccurate. For example, if you set it to 0.5ms, it will sleep for 0.5ms. if you set it to 0.3ms, it will still sleep for 0.5ms.
Compared to sleep()'s minimal precision of 10 milliseconds, this is a commendable improvement.

Re: A more precise sleep()

Posted: 21 Jan 2024, 21:58
by boiler
So while it’s a significant improvement, it’s not really this:
Phantom wrote: Sub-millisecond sleep support…

Re: A more precise sleep()

Posted: 21 Jan 2024, 22:22
by Phantom
boiler wrote:
21 Jan 2024, 21:58
So while it’s a significant improvement, it’s not really this:
Phantom wrote: Sub-millisecond sleep support…
If you set it to sleep for 0.5ms, it sleeps for 0.5ms, isn't that "Sub-millisecond"?

Re: A more precise sleep()

Posted: 21 Jan 2024, 23:03
by iseahound
You need to run tests without any applications in the background and unplug your laptop and enable every power-saving feature possible. Programs like VSCode and Spotify and Chrome/Firefox/Edge are setting the global timer resolution via timeBeginPeriod to 1.

See: https://news.ycombinator.com/item?id=24684813

For an AutoHotkey example on using small sleep resolutions via timeSetEvent, You can call it via ImagePutWindow("animated.gif") here I don't remember exactly what I did to get it to work, but there was a tricky bit about getting it to synchronize OS-level threads.

Re: A more precise sleep()

Posted: 21 Jan 2024, 23:12
by boiler
Phantom wrote: If you set it to sleep for 0.5ms, it sleeps for 0.5ms, isn't that "Sub-millisecond"?
You are the one that just told us “Less than 1 millisecond becomes inaccurate.” Is that considered sub-millisecond support? And you showed a whole series of test results supposedly as a demonstration of this sub-millisecond support, and not one of them was less than a millisecond.

Re: A more precise sleep()

Posted: 21 Jan 2024, 23:38
by Phantom
iseahound wrote:
21 Jan 2024, 23:03
You need to run tests without any applications in the background and unplug your laptop and enable every power-saving feature possible. Programs like VSCode and Spotify and Chrome/Firefox/Edge are setting the global timer resolution via timeBeginPeriod to 1.
So what is the point of this test with no external influence at all? As long as there are other processes present when AutoHotkey.exe is running, the test results are invalid for it.

Re: A more precise sleep()

Posted: 21 Jan 2024, 23:45
by Phantom
Also. If sleep(1000.00000001) sleeps for exactly 1000.00000001s, then it's a sleep that only supports "seconds"?

Re: A more precise sleep()

Posted: 22 Jan 2024, 00:15
by boiler
OP has lost his posting privileges, and his offensive posts have been deleted from the thread.

Re: A more precise sleep()

Posted: 22 Jan 2024, 00:24
by kazhafeizhale
Thank you very much. I've tested it, compared to the built-in sleep,This accuracy can reach 1 ms.

Re: A more precise sleep()

Posted: 22 Jan 2024, 00:53
by crocodile
I recently read something similar in the update log at rust:

https://releases.rs/docs/1.75.0/
Windows: Support sub-millisecond sleep.

Re: A more precise sleep()

Posted: 22 Jan 2024, 02:01
by jNizM
We tried something like this years ago: viewtopic.php?style=17&f=6&t=29957

Acquiring high-resolution time stamps (MSDN)