A class for recording the time an event took place during a script execution. Useful for profiling or benchmarking long running scripts or to simply time the execution time between two points.
Supports "split" times, where a split can be marked with a marker name for later identification. Split times are always recorded as the length of time elapsed since the LAST split. If no previous split has been recorded, the start time is used.
By default all microtimes are rounded to 5 decimal places. To change this, set the precision before starting the timer.
The stop watch currently has 3 status points: STOPPED - the stop watch is not timing RUNNING - the stop watch has been started SPLIT - the stop watch is running and split times are being recorded
The status is set to split whenever a split is recorded. This will affect the return state of utilityStopWatch::elapsed() where the return will be the time elapsed since the last split time.
utilityStopWatch implements the __toString method allowing the timer to be printed by using it as a string. If the current instance of utilityStopWatch is set to running, __toString will only report "running" and not a breakdown. If you require a breakdown stop() the clock first.
Example:
$oTimer = new utilityStopWatch(); $oTimer->setPrecision(3); $oTimer->start(); // do something $oTimer->stop(); echo $oTimer, "\n"; // output string of results
Example using split:
$oTimer = new utilityStopWatch();
$oTimer->start();
$oTimer->split('setup-start');
// do something to set up the script
$oTimer->split('setup-done');
while ( $someThing ) {
// do something complicated and record when finished
$oTimer->split('iteration');
}
$oTimer->stop();
// echo results
echo $oTimer->getResultsAsString(), "\n"; // output string of results
public __construct()
Creates a new stop watch instance
public __toString()
If stop watch is used as a string, output results
public reset()
Resets the object to defaults
public start()
Starts the stop watch
public split([$inMarker = null])
Creates a "split" point, with optional reference $inMarker
public stop()
Stops the stop watch
public elapsed()
Returns the total time elapsed depending on current status
STOPPED: Returns total time elapsed SPLIT: Returns the time elapsed since last split RUNNING: Returns the current running time since stop watch started
public getResultsAsString([$inLineBreak = ' '])
Returns the stop watch results as a formatted string
protected _getTime()
Internal function to return formatted microtime with a certain precision
public getStatus()
Returns $_Status
protected _setStatus($inStatus)
Set $_Status to $inStatus
public getStartTime()
Returns $_StartTime
protected _setStartTime($inStartTime)
Set $_StartTime to $inStartTime
public getEndTime()
Returns $_EndTime
protected _setEndTime($inEndTime)
Set $_EndTime to $inEndTime
public getSplitTimes()
Returns $_SplitTimes
protected _markSplitTime([$inMarker = null])
Record a split time against the event $inMarker, use public method {@link utilityStopWatch::split}
public getLastSplitTime()
Returns the last split time entered on the split stack
protected _getUniqueMarker($inMarker)
Returns a unique reference for $inMarker
public getSplitsMatching($inSplit)
Returns all split times matching $inSplit
public getPrecision()
Returns $_Precision
public setPrecision($inPrecision)
Set $_Precision to $inPrecision
Tags: utility, utilitystopwatch,
utility Articles