Skip to content

Instantly share code, notes, and snippets.

@pete-rai
Created January 11, 2019 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pete-rai/ba3a6b866deb2f1203023b58719e198d to your computer and use it in GitHub Desktop.
Save pete-rai/ba3a6b866deb2f1203023b58719e198d to your computer and use it in GitHub Desktop.
A simple PHP timer class with interim marker support
<?
/*
* 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