phlak / chronometer
Time things
Requires
- php: ^8.1 || ^8.2 || ^8.3 || ^8.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phlak/coding-standards: ^3.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0
- psy/psysh: ^0.12
README
Measure the passing of time -- by, Chris Kankiewicz (@phlak.dev), logo by Caneco
Introduction
Chronometer is a library for statically measuring the passing of time in your code. It's intended to be used for benchmarking code execution time.
Requirements
- PHP >= 8.2
Install with Composer
composer require phlak/chronometer
Using Chronometer
First, import Chronometer.
use PHLAK\Chronometer\Timer;
Then start your timer, run your code, stop the timer and get the elapsed time.
Timer::start(); // do something you want to measure... Timer::stop(); return Timer::elapsed();
After running your timer you will need to reset it before using it again.
Timer::reset();
You may optionally reset the timer when you start it with the $reset
parameter.
Timer::start(reset: true);
Usage
start
Start the timer.
Chronometer\Timer::start( [ $reset = false ] ) : float
Example
Chronometer\Timer::start(); // Returns something like 1538016612.1692
stop
Stop the timer.
Chronometer\Timer::stop( void ) : float
Example
Chronometer\Timer::stop(); // Returns something like 1538016632.7721
addLap
Add a new lap.
Chronometer\Timer::addLap( [ string $description = null ] ) : Chronometer\Lap
Example
$lap = Chronometer\Timer::addLap('The first lap.'); $lap->time // Returns something like 1538016625.492 $lap->duration // Returns something like 7.999922990799 $lap->description // Returns 'The first lap.'
started
Return the timer start time.
Chronometer\Timer::started( void ) : float
Example
Chronometer\Timer::started(); // Returns something like 1538016612.1692
stopped
Return the timer stop time.
Chronometer\Timer::stopped( void ) : float
Example
Chronometer\Timer::stopped(); // Returns something like 1538016632.7721
elapsed
Return the total time elapsed in seconds.
Chronometer\Timer::elapsed( void ) : float
Example
Chronometer\Timer::elapsed(); // Returns something like 20.602929115295
lastLap
Return the last lap.
Chronometer\Timer::lastLap( void ) : Chronometer\Lap
Example
$lap = Chronometer\Timer::lastLap(); $lap->time // Returns something like 1538016632.7721 $lap->duration // Returns something like 7.2800490856171
laps
Return an array of all laps.
Chronometer\Timer::laps( void ) : array
Example
Chronometer\Timer::laps(); // Returns an array of Lap objects
reset
Reset the timer state.
Chronometer\Timer::reset( void ) : void
Example
Chronometer\Timer::reset();
Changelog
A list of changes can be found on the GitHub Releases page.
Troubleshooting
For general help and support join our GitHub Discussion or reach out on Bluesky.
Please report bugs to the GitHub Issue Tracker.
Copyright
This project is licensed under the MIT License.