* Copyright 2001-2012 Strangecode, LLC
*
* This file is part of The Strangecode Codebase.
*
* The Strangecode Codebase 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 3 of the License, or (at your option)
* any later version.
*
* The Strangecode Codebase 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
* The Strangecode Codebase. If not, see .
*/
/**
* ScriptTimer.inc.php
*/
class ScriptTimer
{
public $time_format = '%.4F';
protected $_timing_start_times = array();
protected $_timing_stop_times = array();
protected $_timing_cumulative_times = array();
public function start($name='default')
{
$this->_timing_start_times[$name] = explode(' ', microtime());
}
public function stop($name='default')
{
if (!isset($this->_timing_stop_times[$name]) || !isset($this->_timing_cumulative_times[$name])) {
$this->_timing_stop_times[$name] = null;
$this->_timing_cumulative_times[$name] = null;
}
$this->_timing_stop_times[$name] = explode(' ', microtime());
$this->_timing_cumulative_times[$name] += $this->getTime($name);
}
public function getTime($name='default')
{
if (!isset($this->_timing_start_times[$name])) {
return 0;
}
if (!isset($this->_timing_stop_times[$name])) {
$stop_time = explode(' ', microtime());
} else {
$stop_time = $this->_timing_stop_times[$name];
}
// Do the big numbers first so the small ones aren't lost.
$current = $stop_time[1] - $this->_timing_start_times[$name][1];
$current += $stop_time[0] - $this->_timing_start_times[$name][0];
return sprintf($this->time_format, $current);
}
public function printAll($sort_by_time=false)
{
$names = array_map('strlen', array_keys($this->_timing_start_times));
sort($names);
$name_len = end($names);
if ($sort_by_time) {
arsort($this->_timing_cumulative_times, SORT_NUMERIC);
}
$this->_timing_cumulative_times['TOTAL'] = array_sum($this->_timing_cumulative_times);
echo '
';
foreach ($this->_timing_cumulative_times as $name => $time) {
printf("\n%-{$name_len}s $this->time_format", $name, $time);
}
echo '
';
}
}