Jump to content

PHP: Timer Class

- - - - -

  • Please log in to reply
4 replies to this topic

#1
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
<?php
/**
 * This class allows a user to to determine execution 
 * time of code blocks.
 *
 * @author John Ciacia <Sidewinder@extreme-hq.com>
 * @version 1.0
 * @copyright Copyright © 2007, John Ciacia
 * @license [url=http://opensource.org/licenses/gpl-license.php]Open Source Initiative OSI - The GPL:Licensing | Open Source Initiative[/url] GNU Public License
 */ 
 
class Timer {
	var $starttime   = 0;
	var $endtime     = 0;
	var $elapsed     = 0;
	var $timername   = "Not Named";
	
	public function __construct() {

	}
	
	public function start() {
		$this->starttime = $this->_time();
	}
	
	public function stop() {
		$this->endtime = $this->_time();
		$this->_compute();	
	}
	
	public function clear() {
		$this->starttime   = 0;
		$this->endtime     = 0;
		$this->elapsed     = 0;
		$this->timername   = "Not Named";
	}
	
	public function elapsed() {
		return $this->elapsed;
	}
	
	public function settimername($name) {
		$this->timername = $name;
	}
	
	public function gettimername() {
		return $this->timername;
	}
	
	private function _time() {
		$mtime = microtime(); 
		$mtime = explode(' ', $mtime); 
		$mtime = $mtime[1] + $mtime[0]; 
		return $mtime; 
	}
	
	private function _compute() {
		$this->elapsed = (($this->endtime) - ($this->starttime));
	}


}

?>

Edited by John, 11 August 2008 - 03:17 PM.


#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It looks really good, though I'm not able to try your code out. For some reason EasyPHP don't support public- and private-declarations.

I've some small suggestions to your code. Now when you're working with a class, then why don't you encapsulate $starttime, $endtime, $elapsed and $timername? This would give your code a better design, and the user will not be able to reach the variables directly, they've to use the functions. Beside that, you usually don't initialize the variables where you're doing it. It's done in the constructor (__construct())

#3
mgob

mgob

    Newbie

  • Members
  • Pip
  • 1 posts
Very nice, I've got this working flawlessly :)

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
I'm glad you could make use of it. Just out of curiosity - what did you use it for?

#5
Jay Mee

Jay Mee

    Newbie

  • Members
  • Pip
  • 1 posts
Hi there everyone can some one help me with finding or creating a count up timer so i can see how long a page is open and store on my database so that i can see what pages are more popular this sort of code i have been looking for and have not come across it and so need it, all i can find is execution times (load times for pages) i think there pointless becase its uninteresting and pointless as they all load within 1 second or less.


please help if you can, thank you.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users