On the heels of Pickle's timer class...
Posted: Fri Dec 21, 2007 6:12 pm
Thought I'd throw mine out. I didn't want to hijack his thread. This was coded for my framework I am developing.
Notes:
Notes:
- This is a PHP 5 class. It will not work in a PHP 4 environment. No, I will not port it. But you are more than welcome to if you want.
- I have considered adding a stopAndRestart method, but have not needed it as of yet so have not put that in (though it is fairly trivial to do).
Code: Select all
<?php
/**
* Padlock - PHP Application Developers Library and Opensource Code Kit
*
* @category Padlock
* @package Padlock
* @author Robert Gonzalez <robert@everah.com>
* @copyright Copyright (c) 2007, Everah Media Services.
* @license http://creativecommons.org/licenses/by/3.0/ Creative Commons Attribution License 3.0
* @version $Id: Timer.php 93 2007-12-11 14:59:55Z robert $
*/
/**
* Padlock Timer - Manages timing of process
*
* This class handles just about all aspects of keeping time using a method
* similar to microtime(true) - microtime(true).
*
* Usage:
* <code><?php
* $timer = Padlock::getObject('Padlock_Timer');
* $timer->startTimer('page');
* $timer->startTimer('hello');
* sleep(3);
* echo 'Hello World.';
* $timer->stopTimer('hello');
* echo 'Total time for hello was ' . $timer->getTotalTime('hello');
* // more code
* echo 'Page execution time was ' . $timer->getTotalTime('page') . ' seconds';
* ?></code>
*
* @author Robert Gonzalez <robert@everah.com>
* @category Padlock
* @package Padlock
* @version @package_version@
*/
class Padlock_Timer {
/**
* Accuracy measurement for decimal places
*
* @access protected
* @var integer
*/
protected $accuracy = 5;
/**
* The array of timers used by the class
*
* @access private
* @var array
*/
private $timers = array();
/**
* The class constructor
*
* Note that this does not do any timing at all upon instantiation. This merely
* sets the object. It can very easily be coded to start a timer upon
* instantiation.
*
* @access private
*/
public function __construct() {
// To start a timer upon construction, simply add an argument to the
// constructor called $name. Default that to 'default' then start a new
// timer based on that name. Something a la
// $this->startTimer($name);
}
/**
* Sets the accuracy measurement for the rendered float value of the timer
*
* @access public
* @param integer $length
*/
public function setAccuracy($length = 5) {
if (is_numeric($length)) {
$this->accuracy = intval($length);
}
}
/**
* Timer starter for entry $name
*
* @access public
* @param string $name
*/
public function startTimer($name = 'default') {
if (!isset($this->timers[$name]['start'])) {
$this->timers[$name]['start'] = microtime(true);
}
}
/**
* Timer stopper for entry $name
*
* @access public
* @param string $name
*/
public function stopTimer($name = 'default') {
if (!isset($this->timers[$name]['stop'])) {
$this->timers[$name]['stop'] = microtime(true);
}
}
/**
* Timer totalizer for entry $name
*
* @access public
* @param string $name
*/
public function totalTime($name = 'default') {
if (!isset($this->timers[$name]['start'])) {
return (bool) false;
}
if (isset($this->timers[$name]['stop'])) {
$stop_time = $this->timers[$name]['stop'];
} else {
$stop_time = $this->stopTimer($name);
}
// do the big numbers first so the small ones aren't lost
$this->timers[$name]['total'] = $stop_time - $this->timers[$name]['start'];
}
/**
* Timer total fetcher for entry $name
*
* @access public
* @param string $name
* @return float The time, in milliseconds, of execution or false on failure
*/
public function getTotalTime($name = 'default') {
if (!isset($this->timers[$name]['start'])) {
return (bool) false;
}
if (!isset($this->timers[$name]['total'])) {
$this->totalTime($name);
}
return sprintf("%.{$this->accuracy}f", $this->timers[$name]['total']);
}
}
/********************************************************************
* TESTS
*
* Note: Setting an accuracy is not required. It is only shown as
* an example that is can be done, and if it is, it should be done
* after the time calculcations so that the timing process is not
* altered.
*******************************************************************/
// A single timed event
/*
$t = new Padlock_Timer();
$t->startTimer();
sleep(2);
$t->stopTimer();
$t->setAccuracy(7);
echo $t->getTotalTime() . "\n";
$t->setAccuracy(12);
echo $t->getTotalTime() ."\n";
*/
// Multiple events
$t1 = 'billy';
$t2 = 'bobby';
$t = new Padlock_Timer();
$t->startTimer($t1);
$t->startTimer($t2);
sleep(2);
$t->stopTimer($t2);
sleep(3);
$t->stopTimer($t1);
$t->setAccuracy(7);
echo $t->getTotalTime($t1) . "\n";
echo $t->getTotalTime($t2) . "\n";
$t->setAccuracy(12);
echo $t->getTotalTime($t1) ."\n";
echo $t->getTotalTime($t2);
?>