Req->Doc #79871 [Opn]: On Windows, php can sleep only in 1ms steps

From: Date: Mon, 20 Jul 2020 08:45:37 +0000
Subject: Req->Doc #79871 [Opn]: On Windows, php can sleep only in 1ms steps
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17718@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79871&edit=1 ID: 79871 Updated by: cmb@php.net Reported by: michael dot vorisek at email dot cz Summary: On Windows, php can sleep only in 1ms steps Status: Open -Type: Feature/Change Request +Type: Documentation Problem -Package: *General Issues +Package: Unknown/Other Function Operating System: Windows PHP Version: 7.4.8 Block user comment: N Private report: N New Comment: > Not sure if it is related with gettimeofday function usage, […] No it is not, since usleep() doesn't do a *busy* wait. Instead the implementation uses SetWaitableTimer()[1] whose documentation states: | The actual timer accuracy depends on the capability of your | hardware. I suggest using the following as test script: <?php for ($i = 0; $i < 100; $i++) { $t0 = hrtime(true); usleep(2); $t1 = hrtime(true); printf("%d\n", $t1-$t0); } ?> On one machine this gives something like: 951400 961400 938200 On another machine I get: 15920800 13570300 14755100 You may get yet different results. Anyhow, I don't think there is anything we can do about that. Note that the current MinGW CRT just uses Sleep() to implement usleep()[2]. [1] <https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setwaitabletimer> [2] <https://github.com/mirror/mingw-w64/blob/16151c441e89081fd398270bb888511ebef6fb35/mingw-w64-crt/misc/mingw_usleep.c#L11-L17> Previous Comments: ------------------------------------------------------------------------ [2020-07-18 12:52:47] michael dot vorisek at email dot cz Description: ------------ On Windows, php can sleep only in 1ms steps. Not sure if it is related with gettimeofday function usage, as microtime() seems to return "at least microseconds steps" but "gettimeofday in reality returns only 1ms steps on Windows" Test script: --------------- $times = []; $last = microtime(true); for ($i = 0; $i < 10000; $i++) { $t = microtime(true); $times[] = ($t - $last) * 1000 * 1000; $last = $t; usleep(2); // same issue... time_nanosleep(0, 2000); } print_r($times); Expected result: ---------------- numbers lower than 1000 / 1 ms Actual result: -------------- Array ( [0] => 0.95367431640625 [1] => 282.04917907715 [2] => 1029.9682617188 [3] => 964.87998962402 [4] => 999.21226501465 [5] => 991.8212890625 [6] => 1002.0732879639 [7] => 1002.0732879639 [8] => 996.82807922363 [9] => 994.20547485352 [10] => 998.97384643555 [11] => 998.97384643555 [12] => 993.96705627441 ... ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79871&edit=1

« previous php.doc.bugs (#17718) next »