Skip to content

Instantly share code, notes, and snippets.

@pete-rai
Created January 11, 2019 11:04

Revisions

  1. pete-rai created this gist Jan 11, 2019.
    61 changes: 61 additions & 0 deletions timer.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?

    /*
    * 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";
    */