[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-doc-bugs
Subject:    [DOC-BUGS] Doc #79871 [Com]: On Windows, php can sleep only in 1ms steps
From:       "michael dot vorisek at email dot cz" <php-bugs () lists ! php ! net>
Date:       2020-07-20 9:38:49
Message-ID: E1jxSG1-0006iB-UQ () bugs ! php ! net
[Download RAW message or body]

Edit report at https://bugs.php.net/bug.php?id=79871&edit=1

 ID:                 79871
 Comment by:         michael dot vorisek at email dot cz
 Reported by:        michael dot vorisek at email dot cz
 Summary:            On Windows, php can sleep only in 1ms steps
 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

 New Comment:

I tested your script also on Linux (but virtualized):
62133
61154
60665
60609
60563

So we should probably update the docs more like:

Depending on hardware, the system may sleep up too several milliseconds longer.


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

-- 
PHP Documentation Bugs Mailing List (https://www.php.net/)
To unsubscribe, visit: https://www.php.net/unsub.php


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic