1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
<?php
// class MicroTimer (issue #146)
// wraps calls to microtime(), calculating the elapsed time and rounding output
//
class MicroTimer {
private $startTime, $stopTime;
// creates and starts a timer
function __construct()
{
$this->startTime = microtime(true);
}
// stops a timer
public function stop()
{
$this->stopTime = microtime(true);
}
// returns the number of seconds from the timer's creation, or elapsed
// between creation and call to ->stop()
public function elapsed()
{
if ($this->stopTime)
return round($this->stopTime - $this->startTime, 4);
return round(microtime(true) - $this->startTime, 4);
}
// called when using a MicroTimer object as a string
public function __toString()
{
return (string) $this->elapsed();
}
}
|