PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

time_sleep_until> <sys_getloadavg
Last updated: Fri, 04 Jul 2008

view this page in

time_nanosleep

(PHP 5)

time_nanosleep — Verzögert die Ausführung um die gegebene Anzahl Sekunden und Nanosekunden

Beschreibung

mixed time_nanosleep ( int $seconds , int $nanoseconds )

Verzögert die Ausführung für die gegebene Anzahl seconds und nanoseconds .

Parameter-Liste

seconds

Ein positiver Integerwert.

nanoseconds

Ein positiver Integerwert kleiner als eine Milliarde.

Rückgabewerte

Gibt bei Erfolg TRUE zurück, im Fehlerfall FALSE.

Wenn die Verzögerung durch ein Signal unterbrochen wurde wird ein Array mit den folgenden Werten zurückgegeben:

  • seconds - die verbleibenden Sekunden
  • nanoseconds - die verbleibenden Nanosekunden

Beispiele

Beispiel #1 time_nanosleep() Beispiel

<?php
// Vorsicht! Verhält sich nicht wie erwartet wenn ein Array
// zurückgegeben wird
if (time_nanosleep(0500000000)) {
    echo 
"Verzögerung um eine halbe Sekunden.\n";
}

// Dies ist besser:
if (time_nanosleep(0500000000) === true) {
    echo 
"Verzögerung um eine halbe Sekunden.\n";
}

// Optimal ist:
$nano time_nanosleep(2100000);

if (
$nano === true) {
    echo 
"Verzögert um 2 Sekunden und  100 Millisekunden.\n";
} elseif (
$nano === false) {
    echo 
"Keine Verzögerung.\n";
} elseif (
is_array($nano)) {
    
$seconds $nano['seconds'];
    
$nanoseconds $nano['nanoseconds'];
    echo 
"Von einem Signal unterbrochen.\n";
    echo 
"Verbleibende Verzögerung: $seconds Sekunden, $nanoseconds Nanosekunden.";
}
?>

Anmerkungen

Hinweis: Diese Funktion ist auf Windows-Plattformen nicht implementiert.



time_sleep_until> <sys_getloadavg
Last updated: Fri, 04 Jul 2008
 
add a note add a note User Contributed Notes
time_nanosleep
b dot andrew at shaw dot ca
04-Jul-2008 09:10
A response to the note below:

Your function is also useless, as the WinNT 32 kernel only functions at a minimum of about 10+ ms (1,000 us), rendering usleep() useless, because usleep uses the C function which is provided by the system (in this case, kernel32.dll).

You'll want to use a function that does not rely on the kernel, but rather something made for precise measurement:

<?php
function usleep_win( $micro_seconds )
{
    if ( @
function_exists( "socket_create" ) && @function_exists( "socket_select" ) )
    {
       
$false = NULL;
       
$socket = array( socket_create( AF_INET, SOCK_RAW, $false ) );
       
socket_select( $false, $false, $socket, 0, $micro_seconds );
        return
true;
    }
    else
    {
        return
false;
    }
}
?>

This function will allow to you sleep for a specified microsecond, although I have measured it to be off by ~5 us.

Again, most of this depends on the hardware in your system. If you _REALLY_ need to be precise to < 10 us, you shouldn't be using WinNT anyways!
fantasysportswire at yahoo dot com
18-Dec-2006 03:27
Just glancing at this - and the note from over a year ago with a implementation for windows.. with 5.0.0 and higher it would be simplier to just do something like......

<?

if (!function_exists('time_nanosleep')) {

function time_nanosleep($seconds, $nanoseconds) {

sleep($seconds);
usleep(round($nanoseconds/100));

return true;

}

}

?>

....off the top of my head - obviously simple enough there should be no mistakes.. but those are the ones that always seem to get ya :( .....
anybody (a) emuxperts.net
21-Aug-2006 04:03
Documentation states that "seconds" must be positive. This is not correct, 0 is possible.

Rather, "seconds" must be non-negative.
m at kufi dot net
13-Aug-2005 02:03
You should take into account, if you use the function replacement down here, the CPU will be in use of 99% for the time of execution...

(A little bit better in this situation is to let the 'full seconds' go by a normal sleep command (makes the thread sleep!, and uses minimum cpu))

<?php
   
//THIS IS THE FUNCTION WE ARE TALKIN ABOUT
   
function timeWait($microtime)
    {
//optimizations added by me [start]
//sleep the full seconds
sleep(intval($microtime));
//set the microtime to only resleep the last part of the nanos
$microtime = $microtime - intval($microtime);
//optimizations added by me [end]

       
$timeLimit = $microtime + array_sum(explode(" ",microtime()));
        while(
array_sum(explode(" ",microtime())) < $timeLimit)
        {
/*DO NOTHING*/}
        return(
true);
    }
 
   
//THIS IS HOW WE CAN USE IT
   
echo "Process started at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.<br>";
   
timeWait(5.5); //With this call the system will wait 5 seconds and a half. You can use either integer or float.
   
echo "Process completed at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.";
 
?>
tecnomaniac at ig dot com dot br
26-Jul-2005 03:04
This is an alternative function to sleep_nanosecond that you can use with PHP versions below PHP 5.0. It is not very accurate if we talk about nanoseconds but the results are satisfatory. Enjoy!

<?php
   
//THIS IS THE FUNCTION WE ARE TALKIN ABOUT
   
function timeWait($microtime)
    {
       
$timeLimit = $microtime + array_sum(explode(" ",microtime()));
        while(
array_sum(explode(" ",microtime())) < $timeLimit)
        {
/*DO NOTHING*/}
        return(
true);
    }

   
//THIS IS HOW WE CAN USE IT
   
echo "Process started at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.<br>";
   
timeWait(5.5); //With this call the system will wait 5 seconds and a half. You can use either integer or float.
   
echo "Process completed at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.";
?>

time_sleep_until> <sys_getloadavg
Last updated: Fri, 04 Jul 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites