TinyTimer - A better PHP microtimer class
Perl PHP
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.pod
TinyTimer.php

README.pod

TinyTimer - A better PHP microtimer class



PHP event timing has never been simpler. Using TinyTimer you can easily start, stop, and even create multiple timers at once. The trick to TinyTimers flexibility is in method chaining and two factory functions.

Create a new timer:
new TinyTimer( boolean $running* );
     *Note: $running is optional & defaults to true.


Create a running timer:
        $time = new TinyTimer();
Create a stopped timer:
        $time = new TinyTimer(false);

To get the time count:
A) Simply echo the TinyTimer (implicitly using overwritten __toString() function).
B) Or use getTime() function.
        echo "Count: $time";
        $count = $time->getTime();


Class Methods:
start() - Start the timer.
stop() - Stop the timer.
restart() - Restart timing by resetting counter.
clear() - Stop timing and clear counter.
addTime(int $aNumber ) - Append to the timing in microseconds.
getTime() - Return the timing in microseconds.
precision(int $aNumber ) - Set float precision.
branch(string 'name', boolean $running* ) - Create a new stopped timer and return parent.
trunk(string 'name', boolean $running* ) - Create and return a new stopped timer.
     *Note: $running is optional & defaults to false.


Simple Example:
        $time = new TinyTimer();        
        sleep(1);
        $time->stop();
        sleep(2);
        echo "time should be 1: $time";

You can create sub timers as properties of a parent timer using branch() and trunk(). Using trunk('name') will return the name property for chaining. Using these functions you can initialize a global timer and many sub timers at once.


Branch and Trunk Example:
        $globalTimer = new newTinyTimer()->trunk('appStart')->branch('eventA')->branch('eventB');

...would create 4 timers.

1) $globalTimer
2) $globalTimer->appStart
3) $globalTimer->appStart->eventA
4) $globalTimer->appStart->eventB