PHP code example of jsanc623 / phpbenchtime

1. Go to this page and download the library: Download jsanc623/phpbenchtime library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jsanc623 / phpbenchtime example snippets

 


PBenchTime\Timer;

$t = new Timer();
$t->start();
sleep(1);
$t->end();

print_r($t->summary());

start(string $name = "start"): void
end(): void
reset(): void
lap(?string $name = null): void
summary(): array
pause(): void
unpause(): void

getState(): TimerState
getStartTime(): float
getEndTime(): float
getPauseTime(): float
getTotalPauseTime(): float
getLaps(): array
getLapCount(): int

$t = new Timer();
$t->start();

sleep(3);

$t->end();

print_r($t->summary());

Array
(
    [running] => -1
    [start]   => 1706812345.1234
    [end]     => 1706812348.1239
    [total]   => 3.0005
    [paused]  => 0
    [laps]    => Array
        (
            [0] => Array
                (
                    [name]  => start
                    [start] => 1706812345.1234
                    [end]   => 1706812348.1239
                    [total] => 3.0005
                )
        )
)

$t = new Timer();
$t->start();

sleep(1);
$t->lap();

sleep(2);
$t->lap();

$t->end();

$t = new Timer();
$t->start('bootstrap');

sleep(1);
$t->lap('database');

sleep(1);
$t->lap('render');

$t->end();

$t = new Timer();
$t->start();

sleep(1);
$t->lap('before pause');

$t->pause();
sleep(3); // excluded from total
$t->unpause();

sleep(1);
$t->lap('after pause');

$t->end();

$t->getTotalPauseTime();