Doc #79871 [Asn->Csd]: On Windows, php can sleep only in 1ms steps
| From: | cmb@php.net | Date: | Mon, 20 Jul 2020 09:12:26 +0000 |
| Subject: | Doc #79871 [Asn->Csd]: On Windows, php can sleep only in 1ms steps | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-17721@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: Assigned
+Status: Closed
Type: Documentation Problem
Package: Unknown/Other Function
Operating System: Windows
PHP Version: 7.4.8
Assigned To: cmb
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2020-07-20 09:12:10] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=be1cedff74c70588ca4d2da2e41179eadf50fa43
Log: Fix #79871: On Windows, php can sleep only in 1ms steps
------------------------------------------------------------------------
[2020-07-20 08:45:36] cmb@php.net
> 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>
------------------------------------------------------------------------
[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