Source for file _timer.class.php
Documentation is available at _timer.class.php
* This file implements the Timer class.
* This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.
* See also {@link http://sourceforge.net/projects/evocms/}.
* @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}.
* Parts of this file are copyright (c)2004-2005 by Daniel HAHLER - {@link http://thequod.de/contact}.
* @license http://b2evolution.net/about/license.html GNU General Public License (GPL)
* b2evolution is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* b2evolution is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with b2evolution; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* Daniel HAHLER grants François PLANQUE the right to license
* Daniel HAHLER's contributions to this file and the b2evolution project
* under any OSI approved OSS license (http://www.opensource.org/licenses/).
* {@internal Below is a list of authors who have contributed to design/coding of this file: }}
* @author blueyed: Daniel HAHLER.
* @version $Id: _timer.class.php,v 1.1.2.1 2005/09/02 21:31:34 fplanque Exp $
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );
* This is a simple class to allow timing/profiling of code portions.
* We store for each category (primary key) the state, start/resume time and the total passed time.
* @param string|NULLIf a category is given the timer starts right away.
function Timer( $category =
NULL )
$this->start( $category );
* Reset a timer category.
function reset( $category )
$this->_times[$category] =
array( 'total' =>
0 );
function start( $category )
debug_log( 'Starting timer '.
$category, 'timer' );
$this->reset( $category );
* Stops a timer category. It may me resumed later on, see {@link resume()}. This is an alias for {@link pause()}.
* @return boolean false, if the timer had not been started.
function stop( $category )
return $this->pause( $category );
* Pauses a timer category. It may me resumed later on, see {@link resume()}.
* NOTE: The timer needs to be started, either through the {@link Timer() Constructor} or the {@link start()} method.
* @return boolean false, if the timer had not been started.
function pause( $category )
if( !isset
($this->_times[$category]['resumed']) )
{ // Timer has not been started!
$this->_times[$category]['total'] +=
$since_pause;
$this->_times[$category]['state'] =
'paused';
* Resumes the timer on a category.
if( !isset
($this->_times[$category]['total']) )
$this->_times[$category]['state'] =
'running';
* Get the time in microseconds that was spent in the given category.
// The timer is running, we need to return the additional time since the last resume.
return $this->_times[$category]['total']
return $this->_times[$category]['total'];
* Get the state a category timer is in.
* @return string 'unknown', 'not initialised', 'running', 'paused'
if( !isset
($this->_times[$category]) )
if( !isset
($this->_times[$category]['state']) )
return 'not initialised';
return $this->_times[$category]['state'];
* Get the current time in microseconds.
return ((float)
$usec + (float)
$sec);
* $Log: _timer.class.php,v $
* Revision 1.1.2.1 2005/09/02 21:31:34 fplanque
* enhanced query debugging features
* Revision 1.1 2005/07/12 23:05:36 blueyed
* Added Timer class with categories 'main' and 'sql_queries' for now.