note 33381 deleted from function.microtime by didou
| From: | didou@php.net | Date: | Mon, 29 Dec 2003 15:20:52 +0000 |
| Subject: | note 33381 deleted from function.microtime by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-62639@lists.php.net to get a copy of this message | ||
Note Submitter: jason@burningdesigns.com
----
This is yet another example of a high-precision timer.
The problem, as previously stated, is that you only get 14 digits of precision in a float in PHP (as
of now - early summer 2003). This leaves only 2 places after the decimal point once you add the SEC
and USEC together.
So what do I do? Grab the current time, round it down to the nearest 100 seconds, and subtract the
full seconds from it before concatenating them. Like so:
function get_elapsed_time($start){
$now = microtime();
list($usec_s,$sec_s)=explode(" ",$start);
list($usec_n,$sec_n)=explode(" ",$now);
$reftime = (round(time() / 100) * 100) + 100;
$sec_s -= $reftime;
$sec_n -= $reftime;
return ((float)$sec_e + (float)$usec_e) - ((float)$sec_s + (float)$usec_s);
}
This fails, of course, if more than 100 seconds has elapsed. In this (very rare) case, just adjust
the rounding time to the nearest 1000 instead of the nearest 100. But honestly, the server will
likely limit your execution time to well under 100 seconds.