|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Livia |
|
4
|
|
|
* Copyright 2017-2019 Charlotte Dunois, All Rights Reserved |
|
5
|
|
|
* |
|
6
|
|
|
* Website: https://charuru.moe |
|
7
|
|
|
* License: https://github.com/CharlotteDunois/Livia/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace CharlotteDunois\Livia\Utils; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provides a simple interface to a timer. |
|
14
|
|
|
*/ |
|
15
|
|
|
class HRTimer { |
|
16
|
|
|
/** |
|
17
|
|
|
* Whether the extension hrtime is installed and loaded. |
|
18
|
|
|
* @var bool |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $hrtime; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Whether we use a PHP version which has the function hrtime. (PHP >= 7.3.0) |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $nativeHrtime; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The timer. |
|
30
|
|
|
* @var \HRTime\StopWatch|int|float |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $timer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The last time we called time. |
|
36
|
|
|
* @var int|float |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $lastTime; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor. |
|
42
|
|
|
*/ |
|
43
|
|
|
function __construct() { |
|
44
|
|
|
$this->hrtime = \extension_loaded('hrtime'); |
|
45
|
|
|
$this->nativeHrtime = \function_exists('hrtime'); |
|
46
|
|
|
|
|
47
|
|
|
if($this->hrtime && !$this->nativeHrtime) { |
|
48
|
|
|
$this->timer = new \HRTime\StopWatch(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns the resolution (the end product of 10^X, positive). Nano for hrtime (native and pecl), micro for fallback. |
|
54
|
|
|
* @return int |
|
55
|
|
|
*/ |
|
56
|
|
|
function getResolution(): int { |
|
57
|
|
|
return ($this->nativeHrtime || $this->hrtime ? 1000000000 : 1000000); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Starts the timer. |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
function start(): void { |
|
65
|
|
|
if($this->nativeHrtime) { |
|
66
|
|
|
$this->timer = \hrtime(true); |
|
67
|
|
|
} elseif($this->hrtime) { |
|
68
|
|
|
$this->timer->start(); |
|
69
|
|
|
} else { |
|
70
|
|
|
$this->timer = \microtime(true); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Stops the timer and returns the elapsed time, in nanoseconds. |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
|
|
function stop(): int { |
|
79
|
|
|
if($this->timer === null) { |
|
80
|
|
|
return 0; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if($this->nativeHrtime) { |
|
84
|
|
|
$elapsed = (int) (\hrtime(true) - $this->timer); |
|
85
|
|
|
} elseif($this->hrtime) { |
|
86
|
|
|
$this->timer->stop(); |
|
87
|
|
|
$elapsed = (int) $this->timer->getElapsedTime(\HRTime\Unit::NANOSECOND); |
|
88
|
|
|
} else { |
|
89
|
|
|
$elapsed = \microtime(true) - $this->timer; |
|
90
|
|
|
$elapsed = \ceil(($elapsed * 1000000000)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return ((int) $elapsed); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the elapsed time since the last `time` call, in nanoseconds. |
|
98
|
|
|
* @return int |
|
99
|
|
|
*/ |
|
100
|
|
|
function time(): int { |
|
101
|
|
|
if($this->nativeHrtime) { |
|
102
|
|
|
if(!$this->lastTime) { |
|
103
|
|
|
$this->lastTime = $this->timer; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$time = \hrtime(true); |
|
107
|
|
|
$elapsed = (int) ($time - $this->lastTime); |
|
108
|
|
|
$this->lastTime = $time; |
|
109
|
|
|
} elseif($this->hrtime) { |
|
110
|
|
|
$this->timer->stop(); |
|
111
|
|
|
$elapsed = (int) $this->timer->getLastElapsedTime(\HRTime\Unit::NANOSECOND); |
|
112
|
|
|
$this->timer->start(); |
|
113
|
|
|
} else { |
|
114
|
|
|
if(!$this->lastTime) { |
|
115
|
|
|
$this->lastTime = $this->timer; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$time = \microtime(true); |
|
119
|
|
|
$elapsed = $time - $this->lastTime; |
|
120
|
|
|
|
|
121
|
|
|
$this->lastTime = $time; |
|
122
|
|
|
$elapsed = \ceil(($elapsed * 1000000000)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return ((int) $elapsed); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|