perf/timing

This package is abandoned and no longer maintained. The author suggests using the jmf/timing package instead.

Allows to retrieve and measure time.

2.0.0 2020-08-09 13:03 UTC

This package is auto-updated.

Last update: 2024-05-09 19:23:42 UTC


README

Timing package.

Installation & Requirements

Install with Composer:

composer require perf/timing

Usage

Clock

Clock allows to retrieve current time and date.

Injecting it in your application will allow to ease the testing of time-related operations (mocking its ClockInterface interface and making its return values predictable).

<?php

use perf\Timing\Clock;

$clock = new Clock();

// Will output something like "2020-01-23"
echo $clock->getDateString();

// Will output something like "15:16:17"
echo $clock->getTimeString();

// Will output something like "2020-01-23 15:16:17"
echo $clock->getDateTimeString();

// Will output something like "123456789"
echo $clock->getTimestamp();

// Will output something like "123456789.0123"
echo $clock->getMicrotime();

Timer

Timer allows to mesure elapsed time (in seconds, with microsecond precision). It can be started ($timer->start()), stopped ($timer->stop()), reset ($timer->reset()), and restarted ($timer->restart()). You can also query elapsed time ($timer->getElapsed()).

<?php

use perf\Timing\Timer;

$timer = new Timer();

sleep(1);

// Will output something like "0.0"
echo $timer->getElapsed();

$timer->start();

sleep(1);

// Will output something like "1.0023456"
echo $timer->getElapsed();

sleep(1);

$timer->stop();

sleep(1);

// Will output something like "2.0034567"
echo $timer->getElapsed();

$timer->restart();

sleep(1);

// Will output something like "1.0023456"
echo $timer->getElapsed();

sleep(1);

// Will output something like "2.0034567"
echo $timer->getElapsed();

$timer->reset();

sleep(1);

// Will output something like "0.0"
echo $timer->getElapsed();