Created
January 11, 2019 11:04
-
-
Save pete-rai/ba3a6b866deb2f1203023b58719e198d to your computer and use it in GitHub Desktop.
A simple PHP timer class with interim marker support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
* A simple PHP timer class with interim marker support | |
* | |
* Released with the karmaware tag - https://pete-rai.github.io/karmaware | |
* | |
* Website : http://www.rai.org.uk | |
* GitHub : https://github.com/pete-rai | |
* LinkedIn : https://uk.linkedin.com/in/raipete | |
* NPM : https://www.npmjs.com/~peterai | |
* | |
*/ | |
class Timer | |
{ | |
protected $start; | |
protected $mark; | |
public function __construct () | |
{ | |
$this->start = $this->mark = self::now (); | |
} | |
public static function now () | |
{ | |
return microtime (true); | |
} | |
public function mark () | |
{ | |
$now = self::now (); | |
$tick = $now - $this->mark; | |
$this->mark = $now; | |
return $tick; | |
} | |
public function full () | |
{ | |
return self::now () - $this->start; | |
} | |
} | |
/* | |
// --- test code only - leave commented out once working | |
$timer = new Timer (); | |
echo number_format ($timer->mark (), 6)."\n"; | |
sleep (1); | |
echo number_format ($timer->mark (), 6)."\n"; | |
sleep (2); | |
echo number_format ($timer->mark (), 6)."\n"; | |
sleep (3); | |
echo number_format ($timer->mark (), 6)."\n"; | |
sleep (5); | |
echo number_format ($timer->mark (), 6)."\n"; | |
echo number_format ($timer->full (), 6)."\n"; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment