-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTinyTimer.php
More file actions
executable file
·81 lines (81 loc) · 1.85 KB
/
TinyTimer.php
File metadata and controls
executable file
·81 lines (81 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
class TinyTimer {
private $timeStart;
private $timeStop;
private $runningTime;
private $precise = 5;
public function __construct($running = true){
if($running === true){
$this->start();
}
return $this;
}
public function __toString(){
$time = (string) number_format($this->getTime(),$this->precise);
return $time;
}
public function addTime($timeAdd){
$this->runningTime += $timeAdd;
return $this;
}
public function getTime(){
$this->__calcTime();
$totalTime = $this->runningTime;
return $totalTime;
}
public function start(){
if (empty($this->timeStart)){
$this->timeStart = microtime(true);
}
return $this;
}
public function stop(){
if (!empty($this->timeStart)){
$this->timeStop = microtime(true);
$this->__calcTime();
}
return $this;
}
public function clear(){
$this->timeStart = 0.0;
$this->timeStop = 0.0;
$this->runningTime = 0.0;
return $this;
}
public function restart(){
$this->timeStart = microtime(true);
$this->timeStop = 0.0;
$this->runningTime = 0.0;
return $this;
}
public function trunk($name, $running = false){
if(empty($this->$name)){
$this->$name = new TinyTimer($running);
}
return $this->$name;
}
public function branch($name, $running = false){
if(empty($this->$name)){
$this->$name = new TinyTimer($running);
}
return $this;
}
public function precision($num = 4){
$this->precise = (int)$num;
}
private function __calcTime(){
if (!empty ($this->timeStart)){
if (!empty ($this->timeStop)){
$this->runningTime += ($this->timeStop - $this->timeStart);
$this->timeStart = 0.0;
$this->timeStop = 0.0;
}
else {
$this->runningTime += (microtime(true) - $this->timeStart);
$this->timeStart = microtime(true);
}
}
return $this->runningTime;
}
}
?>