Source for file Timer.php
Documentation is available at Timer.php
// +------------------------------------------------------------------------+
// +------------------------------------------------------------------------+
// | Copyright (c) 2001-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
// +------------------------------------------------------------------------+
// | This source file is subject to the New BSD license, That is bundled |
// | with this package in the file LICENSE, and is available through |
// | the world-wide-web at |
// | http://www.opensource.org/licenses/bsd-license.php |
// | If you did not receive a copy of the new BSDlicense and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +------------------------------------------------------------------------+
// $Id: Timer.php,v 1.2 2007/05/24 05:23:20 anant Exp $
* Provides timing and profiling information.
* Example 1: Automatic profiling start, stop, and output.
* require_once 'Benchmark/Timer.php';
* $timer = new Benchmark_Timer(TRUE);
* $timer->setMarker('Marker 1');
* Example 2: Manual profiling start, stop, and output.
* require_once 'Benchmark/Timer.php';
* $timer = new Benchmark_Timer();
* $timer->setMarker('Marker 1');
* $timer->display(); // to output html formated
* $profiling = $timer->getProfiling(); // get the profiler info as an associative array
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @author Ludovico Magnocavallo <ludo@sumatrasolutions.com>
* @copyright Copyright © 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
* Auto-start and stop timer.
* Max marker name length for non-html output.
var $maxStringLength = 0;
* Close method. Stop timer and display output.
* @see setMarker(), stop()
* @see setMarker(), start()
* @param string $name Name of the marker to be set.
$this->markers[$name] = $this->_getMicrotime ();
* Returns the time elapsed betweens two markers.
* @param string $start start marker, defaults to "Start"
* @param string $end end marker, defaults to "Stop"
* @return double $time_elapsed time elapsed between $start and $end
function timeElapsed($start = 'Start', $end = 'Stop') {
if ($end == 'Stop' && !isset ($this->markers['Stop'])) {
$this->markers['Stop'] = $this->_getMicrotime ();
$end = isset ($this->markers[$end]) ? $this->markers[$end] : 0;
$start = isset ($this->markers[$start]) ? $this->markers[$start] : 0;
return bcsub ($end, $start, 6 );
* Returns profiling information.
* $profiling[x]['name'] = name of marker x
* $profiling[x]['time'] = time index of marker x
* $profiling[x]['diff'] = execution time from marker x-1 to this marker x
* $profiling[x]['total'] = total execution time up to marker x
$temp = reset($this->markers);
$this->maxStringLength = 0;
foreach ($this->markers as $marker => $time) {
$diff = bcsub ($time, $temp, 6 );
$total = bcadd ($total, $diff, 6 );
$result[$i]['name'] = $marker;
$result[$i]['time'] = $time;
$result[$i]['diff'] = $diff;
$result[$i]['total'] = $total;
$this->maxStringLength = (strlen($marker) > $this->maxStringLength ? strlen($marker) + 1 : $this->maxStringLength);
$result[0 ]['diff'] = '-';
$result[0 ]['total'] = '-';
$this->maxStringLength = (strlen('total') > $this->maxStringLength ? strlen('total') : $this->maxStringLength);
$this->maxStringLength += 2;
* Return formatted profiling information.
* @param boolean $showTotal Optionnaly includes total in output, default no
* @param string $format output format (auto, plain or html), default auto
function getOutput($showTotal = FALSE , $format = 'auto') {
$format = isset ($_SERVER['SERVER_PROTOCOL']) ? 'html' : 'plain';
global $HTTP_SERVER_VARS;
$format = isset ($HTTP_SERVER_VARS['SERVER_PROTOCOL']) ? 'html' : 'plain';
$total = $this->TimeElapsed ();
$out = '<table border="1">'. "\n";
$out .= '<tr><td> </td><td align="center"><b>time index</b></td><td align="center"><b>ex time</b></td><td align="center"><b>%</b></td>'.
'<td align="center"><b>elapsed</b></td><td align="center"><b>%</b></td>'
$this->maxStringLength + ($showTotal ? 70 : 45 ), '-', STR_PAD_LEFT );
$out .= str_pad('marker', $this->maxStringLength) .
($showTotal ? ' '. str_pad("elapsed", 16 ). "perct" : ''). "\n" .
foreach ($result as $k => $v) {
$perc = (($v['diff'] * 100 ) / $total);
$tperc = (($v['total'] * 100 ) / $total);
$out .= "<tr><td><b>" . $v['name'] .
"</b></td><td>" . $v['time'] .
"</td><td>" . $v['diff'] .
"</td><td align=\"right\">" .
$out .= str_pad($v['name'], $this->maxStringLength, ' ') .
8 , ' ', STR_PAD_LEFT ) : '').
$out .= " <tr style='background: silver;'><td><b>total</b></td><td>-</td><td>${total}</td><td>100.00%</td>". ($showTotal ? "<td>-</td><td>-</td>" : ""). "</tr>\n";
$out .= str_pad('total', $this->maxStringLength);
* Prints the information returned by getOutput().
* @param boolean $showTotal Optionnaly includes total in output, default no
* @param string $format output format (auto, plain or html), default auto
function display($showTotal = FALSE , $format = 'auto') {
* Wrapper for microtime().
function _getMicrotime () {
return $microtime[1 ] . substr($microtime[0 ], 1 );
Documentation generated on Fri, 22 Jun 2007 21:30:05 -0400 by phpDocumentor 1.3.2. PEAR Logo Copyright © PHP Group 2004.
|