operating-system/php-measurement-time

Package for measuring execution time in PHP scripts

v1.0.0 2018-12-23 18:34 UTC

This package is auto-updated.

Last update: 2020-02-23 21:22:48 UTC


README

Package for measuring execution time in PHP scripts

Installation

composer require operating-system/php-measurement-time

Usage

Instantiate timer:

use OperatingSystem\Php\Measurement\Timer;

$timer = new Timer();

Usage:

$timer->start();
sleep(1);                                       // sleep for 1s
$timer->stop()->format('s');                    // '1.0008s'

$timer->start();
usleep(100000);                                 // sleep for 100ms
$timer->takeTime('My first task')->format();    // '100.3ms'  
usleep(100000);                                 // sleep for 100ms
$timer->takeTime('My second task')->format();   // '201ms'
$timer->stop('Another task')->format();         // '201.3ms'

$timer->getLastTimeTaken()->format();           // '201.3ms'

$timer->getAllTimesTaken();                     // array(3) {
                                                //   'My first task' =>
                                                //   class Unit\Microtime\Microtime#5 (2) {
                                                //     private $microsecond =>
                                                //     double(100300)
                                                //     public $mapper =>
                                                //     class Unit\Microtime\Mapper#6 (0) {
                                                //     }
                                                //   }
                                                //   'My second task' =>
                                                //   class Unit\Microtime\Microtime#7 (2) {
                                                //     private $microsecond =>
                                                //     double(201000)
                                                //     public $mapper =>
                                                //     class Unit\Microtime\Mapper#8 (0) {
                                                //     }
                                                //   }
                                                //   'Another task' =>
                                                //   class Unit\Microtime\Microtime#9 (2) {
                                                //     private $microsecond =>
                                                //     double(201300)
                                                //     public $mapper =>
                                                //     class Unit\Microtime\Mapper#10 (0) {
                                                //     }
                                                //   }
                                                // }
foreach ($timer->getAllTimesTaken() as $name => $timeTaken) {
    // ...
}

Testing

$ ./vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.