You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+25-6Lines changed: 25 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# PHP Timer
2
2
3
-
[](https://packagist.org/packages/ayesh/php-timer)[](https://packagist.org/packages/ayesh/php-timer)[](https://scrutinizer-ci.com/g/Ayesh/php-timer/?branch=master)[](https://github.com/Ayesh/php-timer/actions)[](https://codecov.io/gh/Ayesh/php-timer)[](https://insight.sensiolabs.com/projects/54bcf54f-5087-45bf-9813-63c79a06a642)[](https://github.com/Ayesh/php-timer)
3
+
[](https://packagist.org/packages/ayesh/php-timer)[](https://packagist.org/packages/ayesh/php-timer)[](https://scrutinizer-ci.com/g/Ayesh/php-timer/?branch=master)[](https://github.com/Ayesh/php-timer/actions)[](https://codecov.io/gh/Ayesh/php-timer)[](https://github.com/Ayesh/php-timer)
4
4
5
5
## Synopsis
6
6
A helper class to calculate how long a particular task took.
@@ -17,17 +17,18 @@ This class is similar to phpunit/php-timer, but not a fork, nor mimic its functi
17
17
18
18
## Prerequisites
19
19
20
-
- PHP 7.2 or later.
20
+
- PHP 8.2 or later.
21
21
22
22
## Installing
23
+
23
24
The simplest way would be to install using [composer](https://getcomposer.org).
25
+
24
26
```bash
25
27
composer require ayesh/php-timer
26
28
```
27
29
28
-
If, for some reason you can't use Composer, or don't want to (oh come on!), you can integrate the class with your current `PSR-4` autoloader by mapping `Ayesh\PHP_TIMER` namespace to the repository's `src` folder.
29
-
30
30
## Usage
31
+
31
32
It is pretty simple to use the timer, with all methods being static, and allowing only 4 methods.
32
33
33
34
#### Start timer
@@ -51,43 +52,57 @@ Attempting to start the timer with a non-string key will throw a `\TypeError` ex
51
52
You can call the `start` method multiple times even if the timer has started. It will not reset the timer.
52
53
53
54
#### Read timer
55
+
54
56
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.
57
+
55
58
```php
56
59
Timer::read(); // Default timer.
57
60
Timer::read('default'); // Default timer.
58
61
Timer::read('something'); // Timer started with key "something".
59
62
```
63
+
60
64
Attempting to read a timer that is not started will throw an `\LogicException` exception.
61
65
62
66
##### Formats
67
+
63
68
You can pass a second argument to let this library make minimal processing for you:
See the formats section below for the formats supported.
75
+
68
76
#### Stop timer
77
+
69
78
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.
79
+
70
80
```php
71
81
Timer::stop(); // Default timer.
72
82
Timer::stop('something'); // Timer started with key "something"
73
83
```
84
+
74
85
Attempting to stop a timer that is not started will throw an `\LogicException` exception.
75
86
76
87
#### Reset timer
88
+
77
89
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.
78
90
Resetting the timer will not make the timer start again. You need to explicitly start the timer again with a `Timer::start()` call.
91
+
79
92
```php
80
93
Timer::reset(); // Default timer.
81
94
Timer::reset('something');
82
95
Timer::resetAll(); // Resets all timers.
83
96
```
97
+
84
98
## Formats
99
+
85
100
Currently, the following formats are provided:
86
101
87
102
-`FORMAT_PRECISE`: Precise timer value, without rounding it. e.g. `0.10180473327637`
88
103
-`FORMAT_MILLISECONDS`: Time in milliseconds, rounded to 2 decimals.
89
104
-`FORMAT_SECONDS`: Time in seconds, rounded to 3 decimals.
90
-
-`FORMAT_HUMAN`: Time in humanreadable format, for example `1.05 minutes`.
105
+
-`FORMAT_HUMAN`: Time in human-readable format, for example `1.05 minutes`.
91
106
92
107
## Examples
93
108
@@ -101,7 +116,9 @@ Currently, the following formats are provided:
0 commit comments