1 | <?php |
---|
2 | /** |
---|
3 | * ScriptTimer.inc.php |
---|
4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
5 | */ |
---|
6 | class ScriptTimer { |
---|
7 | |
---|
8 | var $time_format = '%.10s'; |
---|
9 | var $_timing_start_times = array(); |
---|
10 | var $_timing_stop_times = array(); |
---|
11 | var $_timing_cumulative_times = array(); |
---|
12 | |
---|
13 | function start($name='default') |
---|
14 | { |
---|
15 | $this->_timing_start_times[$name] = explode(' ', microtime()); |
---|
16 | } |
---|
17 | |
---|
18 | function stop($name='default') |
---|
19 | { |
---|
20 | $this->_timing_stop_times[$name] = explode(' ', microtime()); |
---|
21 | $this->_timing_cumulative_times[$name] += $this->getTime($name); |
---|
22 | } |
---|
23 | |
---|
24 | function getTime($name='default') |
---|
25 | { |
---|
26 | if (!isset($this->_timing_start_times[$name])) { |
---|
27 | return 0; |
---|
28 | } |
---|
29 | if (!isset($this->_timing_stop_times[$name])) { |
---|
30 | $stop_time = explode(' ', microtime()); |
---|
31 | } else { |
---|
32 | $stop_time = $this->_timing_stop_times[$name]; |
---|
33 | } |
---|
34 | // Do the big numbers first so the small ones aren't lost. |
---|
35 | $current = $stop_time[1] - $this->_timing_start_times[$name][1]; |
---|
36 | $current += $stop_time[0] - $this->_timing_start_times[$name][0]; |
---|
37 | return $current; |
---|
38 | } |
---|
39 | |
---|
40 | function printAll($sort_by_time=false) |
---|
41 | { |
---|
42 | $names = array_map('strlen', array_keys($this->_timing_start_times)); |
---|
43 | sort($names); |
---|
44 | $name_len = end($names); |
---|
45 | |
---|
46 | if ($sort_by_time) { |
---|
47 | arsort($this->_timing_cumulative_times, SORT_NUMERIC); |
---|
48 | } |
---|
49 | |
---|
50 | $this->_timing_cumulative_times["TOTAL"] = array_sum($this->_timing_cumulative_times); |
---|
51 | |
---|
52 | echo '<pre>'; |
---|
53 | foreach ($this->_timing_cumulative_times as $name => $time) { |
---|
54 | printf("\n%-{$name_len}s $this->time_format", $name, $time); |
---|
55 | } |
---|
56 | echo '</pre>'; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | ?> |
---|