Timer
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.
Application Timer
The Hazaar\Application class has a built-in timer for measuring the performance of your application automatically. By default this timer is disabled. See the app.timer
setting in Config Directives.
- Full name:
\Hazaar\Timer
Properties
timers
Array of timers.
private array<string,array<string,float>> $timers
This is an associative array of timers. The key is the name of the timer and the value is an array with the
- start: The time the timer was started
- stop: The time the timer was stopped
If the timer is currently running, the stop key will not exist.
last
The name of the last timer. Used in checkpointing.
private string $last
precision
Timer Precision.
private int $precision
This sets the precision of the output. By default it is 2 which means output is 1/10th of a millisecond.
Methods
__construct
Timer class constructor.
public __construct(int $precision = 2, ?float $start = null): mixed
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:
Parameter | Type | Description |
---|---|---|
$precision | int | The precision to use when returning timer values. Defaults to 2. |
$start | ?float |
__toString
Magic toString method.
public __toString(): string
This will return a string representation of the current state of the default timer object.
start
Start a new timer.
public start(string $name = 'default', float $when = null): void
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:
Parameter | Type | Description |
---|---|---|
$name | string | a name for the timer |
$when | float | Optionally allow the start time to be overriden. Defaults to PHP's microtime(true) value. |
stop
Stop a currently running timer and return it's value.
public stop(mixed $name = 'default'): float
Parameters:
Parameter | Type | Description |
---|---|---|
$name | mixed | The name of the timer to stop. If the timer does not exist an exception is thrown. |
Return Value:
the difference, in milliseconds, between when the timer was started and when it was stopped
checkpoint
Create a timer checkpoint.
public checkpoint(mixed $name): void
Simply put, this will automatically stop the last timer and start a new one in one function call.
Parameters:
Parameter | Type | Description |
---|---|---|
$name | mixed | the name of the new timer |
get
Get the current state of a timer.
public get(string $name = 'default', int $precision = null): float
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.
Parameters:
Parameter | Type | Description |
---|---|---|
$name | string | The name of the timer to stop. If the timer does not exist an exception is thrown. |
$precision | int | The precision of the returned value. If not specified the precision used in the constructor is used. |
all
Get an array of all timers and their current state.
public all(int $precision = null): float[]
Parameters:
Parameter | Type | Description |
---|---|---|
$precision | int | The precision of the returned values. If not specified the precision used in the constructor is used. |
Automatically generated on 2024-11-14