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: namespace Jyxo;
15:
16: /**
17: * Timer for debugging purposes. Allows measuring multiple events simultaneously.
18: *
19: * @category Jyxo
20: * @package Jyxo\Timer
21: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
22: * @license https://github.com/jyxo/php/blob/master/license.txt
23: * @author Štěpán Svoboda
24: * @author Matěj Humpál
25: */
26: final class Timer
27: {
28: /**
29: * Array of start times.
30: *
31: * @var array
32: */
33: private static $starts = array();
34:
35: /**
36: * Starts measuring. Returns timer name.
37: *
38: * @param string $name Custom timer name
39: * @return string
40: */
41: public static function start($name = '')
42: {
43: $start = microtime(true);
44: if (empty($name)) {
45: $name = md5($start . rand(0, 100));
46: }
47: self::$starts[$name] = $start;
48: return $name;
49: }
50:
51: /**
52: * Returns the time difference in seconds.
53: *
54: * @param string $name Timer name
55: * @return float
56: */
57: public static function stop($name)
58: {
59: if (isset(self::$starts[$name])) {
60: $delta = microtime(true) - self::$starts[$name];
61: unset(self::$starts[$name]);
62: return $delta;
63: }
64: return 0;
65: }
66:
67: /**
68: * Returns the time form the last function call.
69: * In case of first function call, function returns 0.
70: *
71: * @return float
72: */
73: public static function timer()
74: {
75: static $time = 0;
76: $previousTime = $time;
77: $time = microtime(true);
78:
79: return (0 === $previousTime) ? 0 : ($time - $previousTime);
80: }
81: }
82: