note 50277 added to function.microtime
| From: | DavidGenordII at osu1 dot php dot net | Date: | Wed, 23 Feb 2005 16:13:53 +0000 |
| Subject: | note 50277 added to function.microtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85440@lists.php.net to get a copy of this message | ||
Here is a little more versitile version of the chronometer script below.
/************************************************************
*
* void | float chronometer($timer)
*
* Works exactly like a stop watch, ie. starts if stopped
* and stops if started
*
* Call the function a first time 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 next call
* will start the chronometer again from where it finished.
*
* Multiple timers can be used by creating multiple $timer
* variables.
*
* An example script would be:
*
* chronometer($timer1);
* DO STUFF HERE
* chronometer($timer2);
* chronometer($timer3);
* DO MORE STUFF
* echo chronometer($timer1);
* DO SOMETHING
* echo chronometer($timer3);
* DO SOMETHING
* echo chronometer($timer2);
*
* The timer variables do not need to be declared or
* initialized before use
*
************************************************************/
function chronometer(&$CHRONO_STARTTIME)
{
$now = microtime(TRUE); // float, in _seconds_
if(isset($CHRONO_STARTTIME['running']))
{
if($CHRONO_STARTTIME['running'])
{
/* Stop the chronometer : return the amount of time since it was started,
in ms with a precision of 3 decimal places.
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 */
$CHRONO_STARTTIME['elapsed'] += round($now - $CHRONO_STARTTIME['temp'], 3);
$CHRONO_STARTTIME['running'] = false;
return $CHRONO_STARTTIME['elapsed'];
}
else
{
$CHRONO_STARTTIME['running'] = true;
$CHRONO_STARTTIME['temp'] = $now;
}
}
else
{
// Start the chronometer : save the starting time
$CHRONO_STARTTIME = array();
$CHRONO_STARTTIME['running'] = true;
$CHRONO_STARTTIME['elapsed'] = 0;
$CHRONO_STARTTIME['temp'] = $now;
}
}
----
Manual Page -- http://www.php.net/manual/en/function.microtime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50277
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50277&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50277&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50277&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50277&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50277&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50277&report=yes
Search -- http://master.php.net/manage/user-notes.php