-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Timer.php
67 lines (58 loc) · 1.25 KB
/
Timer.php
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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @version $Id$
*
* @category Piwik
* @package Piwik
*/
/**
*
* @package Piwik
*/
class Piwik_Timer
{
private $timerStart;
private $memoryStart;
public function __construct()
{
$this->init();
}
public function init()
{
$this->timerStart = $this->getMicrotime();
$this->memoryStart = $this->getMemoryUsage();
}
public function getTime($decimals = 3)
{
return number_format($this->getMicrotime() - $this->timerStart, $decimals, '.', '');
}
public function getTimeMs($decimals = 3)
{
return number_format(1000*($this->getMicrotime() - $this->timerStart), $decimals, '.', '');
}
public function getMemoryLeak()
{
return "Memory delta: ".Piwik::getPrettySizeFromBytes($this->getMemoryUsage() - $this->memoryStart);
}
public function __toString()
{
return "Time elapsed: ". $this->getTime() ."s";
}
private function getMicrotime()
{
list($micro_seconds, $seconds) = explode(" ", microtime());
return ((float)$micro_seconds + (float)$seconds);
}
private function getMemoryUsage()
{
if(function_exists('memory_get_usage'))
{
return memory_get_usage();
}
return 0;
}
}