-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimer.php
More file actions
executable file
·57 lines (50 loc) · 1.31 KB
/
Copy pathTimer.php
File metadata and controls
executable file
·57 lines (50 loc) · 1.31 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
<?php
class Base_Util_Timer {
private static $_arrTimerSet = array();
/**
* 返回当前时间戳
*
* @return int
*/
private static function getMicrotime() {
list($usec, $sec) = explode(" ", microtime());
$usec = (float)$usec;
$usec = $usec * 1000;
$sec = (float)$sec;
$sec = ($sec % 10000) * 1000;
$v = intval($usec + $sec);
return $v;
}
/**
* 开始计时
*
* @param string name
* @return null
*/
public static function start($name) {
$name = trim($name);
if (strlen($name)<1) {
throw new Base_Exception_Runtime("time name is empty");
}
self::$_arrTimerSet[$name] = self::getMicrotime();
}
/**
* 结束计时
*
* @param string name
* @return integer ms
*/
public static function stop($name) {
$name = trim($name);
if (strlen($name)<1) {
throw new Base_Exception_Runtime("time name is empty");
}
if (!array_key_exists($name, self::$_arrTimerSet)) {
return 0;
}
$current = self::getMicrotime();
$old = self::$_arrTimerSet[$name];
unset(self::$_arrTimerSet[$name]);
return $current - $old;
}
}