note 49022 added to function.microtime
| From: | exaton at NOSPAM dot free dot fr | Date: | Fri, 14 Jan 2005 15:50:28 +0000 |
| Subject: | note 49022 added to function.microtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83251@lists.php.net to get a copy of this message | ||
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;
}
}
----
Manual Page -- http://www.php.net/manual/en/function.microtime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49022
Delete -- http://master.php.net/manage/user-notes.php?action=delete+49022&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49022&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+49022&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+49022&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+49022&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+49022&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49022&report=yes
Search -- http://master.php.net/manage/user-notes.php