note 126400 added to function.sleep

From: Date: Wed, 08 Sep 2021 08:08:19 +0000
Subject: note 126400 added to function.sleep
Groups: php.notes 
Request: Send a blank email to php-notes+get-216927@lists.php.net to get a copy of this message
I wrote a simple method for sleeping with a float, which also allows you to do milliseconds (via fractional seconds). <?php function sleepFloatSecs($secs) { $intSecs = intval($secs); $microSecs = ($secs - $intSecs) * 1000000; if($intSecs > 0) { sleep($intSecs); } if($microSecs > 0) { usleep($microSecs); } } ?> And testing on my machine it works perfectly: <?php $x = [0.100,0.250,0.5,1.0,1.5,2.0,2.5]; foreach($x as $secs) { $t = microtime(true); sleepFloatSecs($secs); $t = microtime(true) - $t; echo "$secs \t => \t $t\n"; } ?> Output: <?php 0.1 => 0.10017800331116 0.25 => 0.25016593933105 0.5 => 0.50015211105347 1 => 1.0001430511475 1.5 => 1.5003218650818 2 => 2.000167131424 2.5 => 2.5002470016479 ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 45.135.186.59) ---- Manual Page -- https://php.net/manual/en/function.sleep.php Edit -- https://main.php.net/note/edit/126400 Del: integrated -- https://main.php.net/note/delete/126400/integrated Del: useless -- https://main.php.net/note/delete/126400/useless Del: bad code -- https://main.php.net/note/delete/126400/bad+code Del: spam -- https://main.php.net/note/delete/126400/spam Del: non-english -- https://main.php.net/note/delete/126400/non-english Del: in docs -- https://main.php.net/note/delete/126400/in+docs Del: other reasons-- https://main.php.net/note/delete/126400 Reject -- https://main.php.net/note/reject/126400 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216927) next »