|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-07-18 12:52 UTC] 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
...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 02:00:01 2025 UTC |
> 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>