1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Timer for debugging purposes. Allows measuring multiple events simultaneously.
16: *
17: * @category Jyxo
18: * @package Jyxo_Timer
19: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
20: * @license https://github.com/jyxo/php/blob/master/license.txt
21: * @author Štěpán Svoboda
22: * @author Matěj Humpál
23: */
24: final class Jyxo_Timer
25: {
26: /**
27: * Array of start times.
28: *
29: * @var array
30: */
31: private static $starts = array();
32:
33: /**
34: * Starts measuring. Returns timer name.
35: *
36: * @param string $name Custom timer name
37: * @return string
38: */
39: public static function start($name = '')
40: {
41: $start = microtime(true);
42: if (empty($name)) {
43: $name = md5($start . rand(0, 100));
44: }
45: self::$starts[$name] = $start;
46: return $name;
47: }
48:
49: /**
50: * Returns the time difference in seconds.
51: *
52: * @param string $name Timer name
53: * @return float
54: */
55: public static function stop($name)
56: {
57: if (isset(self::$starts[$name])) {
58: $delta = microtime(true) - self::$starts[$name];
59: unset(self::$starts[$name]);
60: return $delta;
61: }
62: return 0;
63: }
64:
65: /**
66: * Returns the time form the last function call.
67: * In case of first function call, function returns 0.
68: *
69: * @return float
70: */
71: public static function timer()
72: {
73: static $time = 0;
74: $previousTime = $time;
75: $time = microtime(true);
76:
77: return (0 === $previousTime) ? 0 : ($time - $previousTime);
78: }
79: }
80: