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
+24-17Lines changed: 24 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -20,61 +20,66 @@ This class is similar to phpunit/php-timer, but not a fork, nor mimic its functi
20
20
21
21
## Installing
22
22
The simplest way would be to install using [composer](https://getcomposer.org).
23
-
23
+
```bash
24
24
composer require ayesh/php-timer
25
+
```
25
26
26
27
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.
27
28
28
29
## Usage
29
30
It is pretty simple to use the timer, with all methods being static, and allowing only 4 methods.
30
31
31
32
#### Start timer
32
-
33
+
```php
34
+
<?php
33
35
use Ayesh\PHP_Timer\Timer;
34
36
Timer::start();
37
+
````
35
38
This starts the timer (actually keeps the current time stored) with the key `default`. Throughout the library, if you do not provide a specific key, this default key is used.
36
39
37
40
Alternately, you can start the timer with a given key:
38
-
41
+
```php
39
42
Timer::start('something');
43
+
```
40
44
Once you start the time with a given key, you can use the same key to refer to that particular timer.
41
45
You can of course use PHP magic constants to make things easier:
42
-
46
+
```php
43
47
Timer::start(__FUNCTION__);
48
+
```
44
49
Attempting to start the timer with a non-string key will throw a `\TypeError` exception.
45
50
You can call the `start` method multiple times even if the timer has started. It will not reset the timer.
46
51
47
52
#### Read timer
48
53
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.
49
-
54
+
```php
50
55
Timer::read(); // Default timer.
51
56
Timer::read('default'); // Default timer.
52
57
Timer::read('something'); // Timer started with key "something".
58
+
```
53
59
Attempting to read a timer that is not started will throw an `\LogicException` exception.
54
60
55
61
##### Formats
56
62
You can pass a second argument to let this library make minimal processing for you:
See the formats section below for the formats supported.
62
67
#### Stop timer
63
68
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.
64
-
69
+
```php
65
70
Timer::stop(); // Default timer.
66
71
Timer::stop('something'); // Timer started with key "something"
67
-
72
+
```
68
73
Attempting to stop a timer that is not started will throw an `\LogicException` exception.
69
74
70
75
#### Reset timer
71
76
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.
72
77
Resetting the timer will not make the timer start again. You need to explicitly start the timer again with a `Timer::start()` call.
73
-
78
+
```php
74
79
Timer::reset(); // Default timer.
75
80
Timer::reset('something');
76
81
Timer::resetAll(); // Resets all timers.
77
-
82
+
```
78
83
## Formats
79
84
Currently, the following formats are provided:
80
85
@@ -85,16 +90,18 @@ Currently, the following formats are provided:
0 commit comments