[1] | 1 | <?php |
---|
| 2 | /** |
---|
[135] | 3 | * ScriptTimer.inc.php |
---|
| 4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
[1] | 5 | */ |
---|
| 6 | class ScriptTimer { |
---|
| 7 | |
---|
[92] | 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 | { |
---|
[1] | 15 | $this->_timing_start_times[$name] = explode(' ', microtime()); |
---|
[92] | 16 | } |
---|
| 17 | |
---|
| 18 | function stop($name='default') |
---|
| 19 | { |
---|
[1] | 20 | $this->_timing_stop_times[$name] = explode(' ', microtime()); |
---|
[92] | 21 | $this->_timing_cumulative_times[$name] += $this->getTime($name); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | function getTime($name='default') |
---|
| 25 | { |
---|
[1] | 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]; |
---|
[92] | 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); |
---|
[42] | 51 | |
---|
[92] | 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 | } |
---|
[1] | 58 | } |
---|
| 59 | |
---|
| 60 | ?> |
---|