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