class to measure time intervals in microseconds. You can take times during a code run and at the end get a time table as HTML or text table. The output will contain total and as delta between each take in microseconds. for a howto go to: http://www.blueshoes.org/howto/Bs_StopWatch.pdf
/************************************************************************* * Stopwatch - class to measure time intervals in microseconds. * * ... well... hey it's a stopwatch, what can I say more about it :-) * You can take times during a code run and at the end get a time table as * HTML or text table. The output will contain total and as delta between * each take in microseconds. * * NOTE: This class makes use of php's microtime(). from the manual: * "This function is only available on operating systems that support the * gettimeofday() system call." * I know that linux and windows do that. Haven't seen anything about other os. * * --sb This class is no more an extension from Bs_Object because I need it in Bs_Object * and I don't want any conflicts. I think we can do this with a basic object like this one. * * @author sam blum <sam at blueshoes dot org> * @copyright blueshoes.org, part of the php application framework * @version 4.2.$id$ * @package util * @access public */ class Bs_StopWatch {
var $_startTime = NULL; // The start time. var $_stops = NULL; // Every call to takeTime() will add an entry to $_stops var $_lastTakeTime = NULL; // Last saved intermediate time var $_lastDeltaTime = NULL; // Last non saved intermediate time
/** * Constructor. * @access public (pseudo static) */ function Bs_StopWatch() { $this->reset(); }
/** * Resets the stopwatch. * @access public * @return void */ function reset() { $this->_lastTakeTime = $this->_lastDeltaTime = $this->_startTime = explode(' ', microtime()); $this->_stops = array(); }
/** * Takes a time and calculates the total time so far and the delta time * since the last take. These values are stored. * @access public * @param string $info Add any info as memo for what the time take stands for. * @return void */ function takeTime($info='') { $now = explode(' ', microtime()); $tot = (round( (($now[BS_STOPWATCH_SW_SEC] - $this->_startTime[BS_STOPWATCH_SW_SEC]) + ($now[BS_STOPWATCH_SW_MSEC] - $this->_startTime[BS_STOPWATCH_SW_MSEC]))*1000 )); $delta = (round( (($now[BS_STOPWATCH_SW_SEC] - $this->_lastTakeTime[BS_STOPWATCH_SW_SEC]) + ($now[BS_STOPWATCH_SW_MSEC] - $this->_lastTakeTime[BS_STOPWATCH_SW_MSEC]))*1000 )); $this->_lastTakeTime = $now; $this->_stops[] = array('INFO'=>$info, 'TOT'=>$tot, 'DELTA'=>$delta); }
/** * Returns total time in ms since reset. * @access public * @return integer Total time in ms since reset. */ function getTime() { $now = explode(' ', microtime()); return (round( (($now[BS_STOPWATCH_SW_SEC] - $this->_startTime[BS_STOPWATCH_SW_SEC]) + ($now[BS_STOPWATCH_SW_MSEC] - $this->_startTime[BS_STOPWATCH_SW_MSEC]))*1000 )); }
/** * Returns total time in ms since last call to getDelta() * @access public * @return integer Total time in ms since since last call. */ function getDelta() { $now = explode(' ', microtime()); $delta = (round( (($now[BS_STOPWATCH_SW_SEC] - $this->_lastDeltaTime[BS_STOPWATCH_SW_SEC]) + ($now[BS_STOPWATCH_SW_MSEC] - $this->_lastDeltaTime[BS_STOPWATCH_SW_MSEC]))*1000 )); $this->_lastDeltaTime = $now; return $delta; }
/** * Displays all stops so far as HTML table. * @access public * @param string $title a title to display * @return string an html table */ function toHtml($title='') { $ret = ''; if ($title != '') $ret .= "<B>{$title}</B><br>";
/** * Displays all stops so far as simple string table. * @access public * @param string $title a title to display * @return string table */ function toString($title='') { $this->_weightIt(); // Do some weighting
if (basename($_SERVER['PHP_SELF']) == 'Bs_StopWatch.class.php') { ################################################################################################### $myStopWatch =& new Bs_StopWatch(); $myStopWatch->reset();
for ($i=0; $i<200000; $i++) {;} // Use some CPU $myStopWatch->takeTime("Take 1"); // Take time
for ($i=0; $i<30000; $i++) {;} // Use some more CPU $myStopWatch->takeTime("Take 2"); // Take time again
for ($i=0; $i<60000; $i++) {;} $myStopWatch->takeTime("Take 3");
for ($i=0; $i<100000; $i++) {;} $myStopWatch->takeTime("Take 4"); // Last time take.
// Output as HTML echo $myStopWatch->toHtml("This is the result of toHtml():");
// Output as Text echo "<br><hr><pre>"; echo $myStopWatch->toString("This is the result of toString():"); echo "</pre><br>";
echo 'and here is the code: <br><hr><br>'; highlight_string(join('', file(__FILE__))); }
?>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.