php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32203 usleep still not working correctly
Submitted: 2005-03-06 07:00 UTC Modified: 2005-03-14 01:00 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: master_tip at hotmail dot com Assigned:
Status: No Feedback Package: Date/time related
PHP Version: 5.0.3 OS: Windows 2000 Sp4
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-03-06 07:00 UTC] master_tip at hotmail dot com
Description:
------------
usleep can be very usefull for slowing loops and prevent them from using to much cpu but this usleep does not just slow is almost stops no mather what value you give it.

look at included reproduce code, i run it as cgi but should be the same results from web or other interface.

here is a test loop of 10000 loops. wich takes on my pc 6ms to complete without usleep. now here comes the strange things.

when i remove usleep it runs in 6ms
when i include usleep but set it to zero it takes 90ms
logical since it still makes the call.

but when i set usleep to 1 microsecond it takes 99sec to complete. a massive increase but okay. now when i set it 
to 1000, it still takes 99sec. although the usleep has multiplied factor 1000 it seems to have no effect. 

and when i increase to 2000 still no effect
not until i set it till 100.000 it has effect. 
it now takes 1000sec(16min) to complete. yes i did lower the  loops to 100 and multiplied the time by 100000 to stay in the same scale.) so the time increased by a factor 10 but the usleep only by a factor 1. and between 1 and 100.000 no diffrence wich is equal to a precision of 100ms, so it does not accept microseconds at all except zero. at a delay of 500000 microseconds the total of 10000 loops took 5000 seconds.

so usleep works more like sleep(0.1) instead of usleep(1000) wich would be sleep(0.01);. there was a bug report earlier from php version 5.0.0 where it was said closed since new cvs snapshot contained the fix. now in php version 5.0.3 usleep still does not work, at least not wich microseconds or milisecond but deciseconds. 

also what if usleep read deciseconds instead of microseconds. because 100000 deciseconds is 100 seconds. 

should be time to get this fixed or removed it from documentation for a later point to be added again.

hope this would help in fixing this problem, especialy for gtk apps its important that php does not use 100% cpu. its not a full-screen game or something.

ps. actual times can be diffrent but its ofcourse about the scale not being correct. 

greetings from holland,
 Docey.


Reproduce code:
---------------
<?php
// get starting time
$start_time_raw = microtime();
$rounds = 0;

// make some loops
while($rounds != 10000){
 $rounds++;
 usleep(0);
}
 // note the quit time
 $end_time_raw = microtime();

 // process time between start and end.
 list($end_time, $end_stime) = split(" ", $end_time_raw, 2);
 list($start_time, $start_stime) = split(" ", $start_time_raw, 2);

 $dif_time = 0;

 if($start_stime < $end_stime){
  $dif_time 	  = $end_stime - $start_stime;  
 }else{
  if($end_time < $start_time){
   $dif_time = 1;
  }
 }

 $end_time_calc = $end_time + $dif_time;  
 $run_time = $end_time_calc - $start_time;
 $ms_time  = $run_time * 1000;

// print actual results
print("\n\n total run time ".$ms_time." miliseconds \n\n =>STR:".$start_time_raw."\n =>END:".$end_time_raw."\n =>RUN:".$run_time."\n =>DIF:".$dif_time);

?>

Expected result:
----------------
that when the usleep increases by factor 10 the time to run would also increase by a factor 10 since there is no other delay in between.

like running a loop of 10 times sleep(1) wich would be the same as running 10 times usleep(1000000).

Actual result:
--------------
usleep does not responds on values below 100ms(100000microseconds) and acts like sleep(0.1).


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-06 07:11 UTC] master_tip at hotmail dot com
a few corrections, it would ofcourse be usleep(0.001) since 1000microseconds are 1ms. btw you could also instead of keeping a 1ms break after each loop do 1sec break every 1000 loops by chunking them together. not nice but same result by doing just sleep.  

greetings,
 docey.
 [2005-03-06 16:13 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-03-14 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC