\Hazaar
Timer
Timer class for measuring how long things take

The timer class can be used to time one or more events and returns how long it took in milliseconds.

Tags

Since

1.0.0

Summary
Methods Properties Constants
all
get
No constants
Properties
$precision
$precision
Timer Precision

This sets the precision of the output. By default it is 2 which means output is 1/10th of a millisecond.

$timers
$timers
Array of timers
Methods
__construct()
__construct($precision = 2)
Timer class constructor

The timer class has an implicit timer that is always active called the ‘global’ timer. This is simply used to record how long the timer class itself has been active.

Parameters

$precisionmixed

The precision to use when returning timer values.

__tostring()
__tostring() : string
Magic toString method

This will return a string representation of the current state of the default timer object.

Returns

string
all()
all() : string[]
Get an array of all timers and their current state

If a timer is currently running, then it’s value will be the difference between when it started and ‘now’.

If a timer has stopped, it’s value will be the difference between when it was started and when it stopped.

Returns

string[]
get()
get($name = 'default', $precision = null) : float
Get the current state of a timer.

Parameters

$name

No description

$precision

No description

Returns

\Hazaar\float
start()
start($name = 'default', $when = null)
Start a new timer.

A timer is nothing more than a named point in time. When a timer starts, no long running code is executed or anything like that and simply the current system time in microseconds is recorded against the timer name. This allows us to later query that timer name and return the difference which will give you the number of milliseconds between two points in time.

Parameters

$namemixed

A name for the timer.

$whenmixed

Optionally allow the start time to be overriden. Defaults to PHP’s microtime(true) value.

stop()
stop($name = 'default') : float
Stop a currently running timer and return it's value

Tags

Throws

Exception

Parameters

$namemixed

The name of the timer to stop. If the timer does not exist an exception is thrown.

Returns

\Hazaar\float

The difference, in milliseconds, between when the timer was started and when it was stopped.