note 80869 deleted from function.microtime by tularis
| From: | tularis@php.net | Date: | Sun, 22 Jun 2008 12:04:02 +0000 |
| Subject: | note 80869 deleted from function.microtime by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-140660@lists.php.net to get a copy of this message | ||
Note Submitter: ifoundthetao at gmail dot com
----
Here is my first object. I made a stopwatch that I think is pretty neat. It uses the microtime
setting with the floating point enabled. I spent a lot of time working with the other version until
it dawned on me that it is accurate to the thousandth of a second which is good enough for all of
the sites that I will be working on for the time being.
<?php
//Details:
// Started Stopped
// ---------------------------------------
// | 0 | 0 | = Has not been used yet, or has been reset
// ---------------------------------------
// | 0 | 1 | = Holding time, but paused
// ---------------------------------------
// | 1 | 0 | = Currently running
// ---------------------------------------
class Stopwatch {
public function __construct() {
$this->started = $this->stopped = $this->timer=0;
}
public function Start() {
//leave the function because it is already running
if($this->started) return;
if(!($this->stopped)) {
$this->started = true;
$this->timer = microtime(true);
}
//stopwatch is currently stopped, begin tracking time again
if($this->stopped) {
$this->timer = microtime(true) - $this->timer;
$this->started = true;
$this->stopped = false;
}
}
public function Stop() {
//make sure that it is running before you stop it
if(!$this->stopped && $this->started) {
$this->timer = microtime(true) - $this->timer;
$this->stopped = true;
$this->started = false;
}
}
public function Reset() {
$this->timer = $this->started = $this->stopped = false;
}
public function Display() {
//still running, use current time.
if(!($this->stopped) && ($this->started)) $sec = microtime(true) - $this->timer;
else if(!$this->stopped && !$this->started) $sec = 0;
else $sec = $this->timer;
echo "<br />Time: $sec seconds<br />";
}
private $timer, $started, $stopped;
}
?>