https://github.com/phlak/chronometer
Measure the passing of time.
https://github.com/phlak/chronometer
library php php-library timer
Last synced: about 1 year ago
JSON representation
Measure the passing of time.
- Host: GitHub
- URL: https://github.com/phlak/chronometer
- Owner: PHLAK
- License: mit
- Created: 2018-09-27T00:51:03.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2025-01-01T11:01:32.000Z (over 1 year ago)
- Last Synced: 2025-04-12T18:12:34.218Z (about 1 year ago)
- Topics: library, php, php-library, timer
- Language: PHP
- Homepage:
- Size: 102 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
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](https://php.net) >= 8.2
Install with Composer
---------------------
```bash
composer require phlak/chronometer
```
Using Chronometer
-----------------
First, import Chronometer.
```php
use PHLAK\Chronometer\Timer;
```
Then start your timer, run your code, stop the timer and get the elapsed time.
```php
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.
```php
Timer::reset();
```
You may optionally reset the timer when you start it with the `$reset` parameter.
```php
Timer::start(reset: true);
```
Usage
-----
### start
> Start the timer.
```php
Chronometer\Timer::start( [ $reset = false ] ) : float
```
#### Example
```php
Chronometer\Timer::start(); // Returns something like 1538016612.1692
```
---
### stop
> Stop the timer.
```php
Chronometer\Timer::stop( void ) : float
```
#### Example
```php
Chronometer\Timer::stop(); // Returns something like 1538016632.7721
```
---
### addLap
> Add a new lap.
```php
Chronometer\Timer::addLap( [ string $description = null ] ) : Chronometer\Lap
```
#### Example
```php
$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.
```php
Chronometer\Timer::started( void ) : float
```
#### Example
```php
Chronometer\Timer::started(); // Returns something like 1538016612.1692
```
---
### stopped
> Return the timer stop time.
```php
Chronometer\Timer::stopped( void ) : float
```
#### Example
```php
Chronometer\Timer::stopped(); // Returns something like 1538016632.7721
```
---
### elapsed
> Return the total time elapsed in seconds.
```php
Chronometer\Timer::elapsed( void ) : float
```
#### Example
```php
Chronometer\Timer::elapsed(); // Returns something like 20.602929115295
```
---
### lastLap
> Return the last lap.
```php
Chronometer\Timer::lastLap( void ) : Chronometer\Lap
```
#### Example
```php
$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.
```php
Chronometer\Timer::laps( void ) : array
```
#### Example
```php
Chronometer\Timer::laps(); // Returns an array of Lap objects
```
---
### reset
> Reset the timer state.
```php
Chronometer\Timer::reset( void ) : void
```
#### Example
```php
Chronometer\Timer::reset();
```
---
Changelog
---------
A list of changes can be found on the [GitHub Releases](https://github.com/PHLAK/Chronometer/releases) page.
Troubleshooting
---------------
For general help and support join our [GitHub Discussion](https://github.com/PHLAK/Chronometer/discussions) or reach out on [Bluesky](https://bsky.app/profile/phlak.dev).
Please report bugs to the [GitHub Issue Tracker](https://github.com/PHLAK/Chronometer/issues).
Copyright
---------
This project is licensed under the [MIT License](https://github.com/PHLAK/Chronometer/blob/master/LICENSE).