A more precise sleep()

Propose new features and changes
Phantom
Posts: 5
Joined: 23 Nov 2023, 08:47

A more precise sleep()

21 Jan 2024, 08:57

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)
}
vmech
Posts: 357
Joined: 25 Aug 2019, 13:03

Re: A more precise sleep()

21 Jan 2024, 10:58

Your math is wrong. Smallest time interval you shown is 1 millisecond.
Please post your script code inside [code] ... [/code] block. Thank you.
Phantom
Posts: 5
Joined: 23 Nov 2023, 08:47

Re: A more precise sleep()

21 Jan 2024, 20:41

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.
User avatar
boiler
Posts: 16983
Joined: 21 Dec 2014, 02:44

Re: A more precise sleep()

21 Jan 2024, 21:58

So while it’s a significant improvement, it’s not really this:
Phantom wrote: Sub-millisecond sleep support…
Phantom
Posts: 5
Joined: 23 Nov 2023, 08:47

Re: A more precise sleep()

21 Jan 2024, 22:22

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"?
iseahound
Posts: 1447
Joined: 13 Aug 2016, 21:04
Contact:

Re: A more precise sleep()

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.

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.
User avatar
boiler
Posts: 16983
Joined: 21 Dec 2014, 02:44

Re: A more precise sleep()

21 Jan 2024, 23:12

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.
Phantom
Posts: 5
Joined: 23 Nov 2023, 08:47

Re: A more precise sleep()

21 Jan 2024, 23:38

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.
Phantom
Posts: 5
Joined: 23 Nov 2023, 08:47

Re: A more precise sleep()

21 Jan 2024, 23:45

Also. If sleep(1000.00000001) sleeps for exactly 1000.00000001s, then it's a sleep that only supports "seconds"?
User avatar
boiler
Posts: 16983
Joined: 21 Dec 2014, 02:44

Re: A more precise sleep()

22 Jan 2024, 00:15

OP has lost his posting privileges, and his offensive posts have been deleted from the thread.
kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: A more precise sleep()

22 Jan 2024, 00:24

Thank you very much. I've tested it, compared to the built-in sleep,This accuracy can reach 1 ms.
crocodile
Posts: 98
Joined: 28 Dec 2020, 13:41

Re: A more precise sleep()

22 Jan 2024, 00:53

I recently read something similar in the update log at rust:

https://releases.rs/docs/1.75.0/
Windows: Support sub-millisecond sleep.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A more precise sleep()

22 Jan 2024, 02:01

We tried something like this years ago: viewtopic.php?style=17&f=6&t=29957

Acquiring high-resolution time stamps (MSDN)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 50 guests