2017 © Pedro Peláez
 

library php-timer

Stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

image

ayesh/php-timer

Stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

  • Tuesday, March 27, 2018
  • by Ayesh
  • Repository
  • 2 Watchers
  • 4 Stars
  • 4,859 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 11 Versions
  • 31 % Grown

The README.md

PHP Timer

Latest Stable Version License Scrutinizer Code Quality CI codecov Too many badges, (*1)

Synopsis

A helper class to calculate how long a particular task took., (*2)

This class is similar to phpunit/php-timer, but not a fork, nor mimic its functionality., (*3)

  • Multiple timers by a given key.
  • Read the current time elapsed without stopping the timer.
  • Stop the timer, and continue from where it left (stopwatch).
  • Dead simple API with only 4 static methods.
  • 100% unit test coverage.
  • Gets you precise time in milliseconds (with options to convert to seconds)
  • Individual Stopwatch class for serialization and other use cases.

Prerequisites

  • PHP 8.2 or later.

Installing

The simplest way would be to install using composer., (*4)

    composer require ayesh/php-timer

Usage

It is pretty simple to use the timer, with all methods being static, and allowing only 4 methods., (*5)

Start timer

    <?php
    use Ayesh\PHP_Timer\Timer;
    Timer::start();
````
This starts the timer (actually keeps the current time stored) with the key `default`. Throughout the library, if you do not provide a specific key, this default key is used.

Alternately, you can start the timer with a given key:
```php
    Timer::start('something');

Once you start the time with a given key, you can use the same key to refer to that particular timer. You can of course use PHP magic constants to make things easier:, (*6)

    Timer::start(__FUNCTION__);

Attempting to start the timer with a non-string key will throw a \TypeError exception. You can call the start method multiple times even if the timer has started. It will not reset the timer., (*7)

Read timer

After starting the timer, you can read the elapsed time at any time. Reading the time will not stop the timer. You can read the timer, do some expensive calculations, and read again to get the cumulative time., (*8)

    Timer::read(); // Default timer.
    Timer::read('default'); // Default timer.
    Timer::read('something'); // Timer started with key "something".

Attempting to read a timer that is not started will throw an \LogicException exception., (*9)

Formats

You can pass a second argument to let this library make minimal processing for you:, (*10)

    Timer::read('something', Timer::FORMAT_PRECISE); // 0.10180473327637

See the formats section below for the formats supported., (*11)

Stop timer

You can stop the timer anytime as well. This makes the library store the stop time, and your further Timer::read() calls will always return the time it took between start and stop., (*12)

    Timer::stop(); // Default timer.
    Timer::stop('something'); // Timer started with key "something"

Attempting to stop a timer that is not started will throw an \LogicException exception., (*13)

Reset timer

By default, starting the timer after stopping it will continue it from where it left off. For example, if you have 3 seconds on the timer when you stop it, and start it again, the total time will start from 3 seconds. You can explicitly reset the timer to make it start from 0. Resetting the timer will not make the timer start again. You need to explicitly start the timer again with a Timer::start() call., (*14)

    Timer::reset(); // Default timer.
    Timer::reset('something');
    Timer::resetAll(); // Resets all timers.

Formats

Currently, the following formats are provided:, (*15)

  • FORMAT_PRECISE: Precise timer value, without rounding it. e.g. 0.10180473327637
  • FORMAT_MILLISECONDS: Time in milliseconds, rounded to 2 decimals.
  • FORMAT_SECONDS: Time in seconds, rounded to 3 decimals.
  • FORMAT_HUMAN: Time in human-readable format, for example 1.05 minutes.

Examples

Calculate the timer one-off:

```php <?php use Ayesh\PHP_Timer\Timer;, (*16)

Timer::start();
// do your processing here.
$time = Timer::read('default', Timer::FORMAT_SECONDS);
echo "Script took {$time} second(s)";

#### Stop watch functionality, with stop-and-go timer calculated separately. ```php <?php use Ayesh\PHP_Timer\Timer; Timer::start('full'); Timer::start('laps'); sleep(1); Timer::stop('laps'); sleep(2); // This time is not calculated under 'laps' Timer::start('laps'); sleep(1); Timer::stop('laps'); echo Timer::read('full', Timer::FORMAT_SECONDS); // 4 seconds. echo "<br />"; echo Timer::read('laps', Timer::FORMAT_SECONDS); // 2 seconds (1 + 1)

Development and tests

All issues are PRs are welcome. Travis CI and PHPUnit tests are included. If you are adding new features, please make sure to add the test coverage., (*17)

Credits

By Ayesh Karunaratne and contributors., (*18)

kthxbye, (*19)

The Versions

27/03 2018

dev-master

9999999-dev https://github.com/ayesh/php-timer

Stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

time timer stopwatch benchmark performance

20/01 2018

v2.x-dev

2.9999999.9999999.9999999-dev https://github.com/ayesh/php-timer

Stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

time timer stopwatch benchmark performance

13/01 2018

dev-infection

dev-infection https://github.com/ayesh/php-timer

Stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

time timer stopwatch benchmark performance

13/01 2018

v1.x-dev

1.9999999.9999999.9999999-dev https://github.com/ayesh/php-timer

Stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

time timer stopwatch benchmark performance

24/07 2017

v1.1.5

1.1.5.0 https://github.com/ayesh/php-timer

Stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

time timer stopwatch benchmark performance

30/03 2017

v1.1.4

1.1.4.0 https://github.com/ayesh/php-timer

Timer start, stop, read, and minimal conversion. Not phpunit/php-timer I promise.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

time timer stopwatch benchmark performance

29/03 2017

v1.1.2

1.1.2.0 https://github.com/ayesh/php-timer

Timer start, stop, read, and minimal conversion. Not phpunit/php-timer I promise.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

time timer stopwatch benchmark performance

29/03 2017

v1.1.1

1.1.1.0 https://github.com/ayesh/php-timer

Timer start, stop, read, and minimal conversion. Not phpunit/php-timer I promise.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

time timer stopwatch benchmark performance

29/03 2017

v1.1.0

1.1.0.0 https://github.com/ayesh/php-timer

Timer start, stop, read, and minimal conversion. Not phpunit/php-timer I promise.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

time timer stopwatch benchmark performance

12/02 2017

1.0.1

1.0.1.0

Timer start, stop, read, and minimal conversion. Not phpunit/php-timer I promise.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

12/02 2017

1.0.0

1.0.0.0

Timer start, stop, read, and minimal conversion. Not phpunit/php-timer I promise.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires