note 49022 deleted from function.microtime by tularis
| From: | tularis@php.net | Date: | Sun, 22 Jun 2008 12:16:41 +0000 |
| Subject: | note 49022 deleted from function.microtime by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-140712@lists.php.net to get a copy of this message | ||
Note Submitter: exaton at NOSPAM dot free dot fr
----
Here's what I use to time my scripts.
There's a bit of surrounding code in the function, which will induce errors in short cycles ;
this should therefore be used in repetitive context where the error will be amortized (cf. also mcq
at supergamez dot hu 's comment below).
Note however that the important thing, $now = microtime(TRUE), happens at the very beginning, not
suffering from any extraneous delay, and following it at the chronometer start is only one little
condition, and a particularly compiler-friendly one at that (I think).
Hope this does not turn out too ugly after wordwrap()...
/************************************************************
*
* void | float chronometer()
*
* Enables determination of an amount of time between two points in a script,
* in milliseconds.
*
* Call the function a first time (as void) to start the chronometer. The next
* call to the function will return the number of milliseconds elapsed since
* the chronometer was started (rounded to three decimal places).
*
* The chronometer is then available for being started again.
*
************************************************************/
$CHRONO_STARTTIME = 0;
function chronometer()
{
global $CHRONO_STARTTIME;
$now = microtime(TRUE); // float, in _seconds_
if ($CHRONO_STARTTIME > 0)
{
/* Stop the chronometer : return the amount of time since it was started,
in ms with a precision of 3 decimal places, and reset the start time.
We could factor the multiplication by 1000 (which converts seconds
into milliseconds) to save memory, but considering that floats can
reach e+308 but only carry 14 decimals, this is certainly more precise */
$retElapsed = round($now * 1000 - $CHRONO_STARTTIME * 1000, 3);
$CHRONO_STARTTIME = 0;
return $retElapsed;
}
else
{
// Start the chronometer : save the starting time
$CHRONO_STARTTIME = $now;
}
}