[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[376] | 5 | * Copyright 2001-2010 Strangecode, LLC |
---|
[362] | 6 | * |
---|
| 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
| 13 | * |
---|
| 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[136] | 24 | * ScriptTimer.inc.php |
---|
[1] | 25 | */ |
---|
| 26 | class ScriptTimer { |
---|
| 27 | |
---|
[248] | 28 | var $time_format = '%.3f'; |
---|
[92] | 29 | var $_timing_start_times = array(); |
---|
| 30 | var $_timing_stop_times = array(); |
---|
| 31 | var $_timing_cumulative_times = array(); |
---|
| 32 | |
---|
| 33 | function start($name='default') |
---|
| 34 | { |
---|
[1] | 35 | $this->_timing_start_times[$name] = explode(' ', microtime()); |
---|
[92] | 36 | } |
---|
| 37 | |
---|
| 38 | function stop($name='default') |
---|
| 39 | { |
---|
[172] | 40 | if (!isset($this->_timing_stop_times[$name]) || !isset($this->_timing_cumulative_times[$name])) { |
---|
| 41 | $this->_timing_stop_times[$name] = null; |
---|
| 42 | $this->_timing_cumulative_times[$name] = null; |
---|
| 43 | } |
---|
[1] | 44 | $this->_timing_stop_times[$name] = explode(' ', microtime()); |
---|
[92] | 45 | $this->_timing_cumulative_times[$name] += $this->getTime($name); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | function getTime($name='default') |
---|
| 49 | { |
---|
[1] | 50 | if (!isset($this->_timing_start_times[$name])) { |
---|
[172] | 51 | return 0; |
---|
[1] | 52 | } |
---|
| 53 | if (!isset($this->_timing_stop_times[$name])) { |
---|
| 54 | $stop_time = explode(' ', microtime()); |
---|
| 55 | } else { |
---|
| 56 | $stop_time = $this->_timing_stop_times[$name]; |
---|
| 57 | } |
---|
| 58 | // Do the big numbers first so the small ones aren't lost. |
---|
| 59 | $current = $stop_time[1] - $this->_timing_start_times[$name][1]; |
---|
| 60 | $current += $stop_time[0] - $this->_timing_start_times[$name][0]; |
---|
[92] | 61 | return $current; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | function printAll($sort_by_time=false) |
---|
| 65 | { |
---|
| 66 | $names = array_map('strlen', array_keys($this->_timing_start_times)); |
---|
| 67 | sort($names); |
---|
| 68 | $name_len = end($names); |
---|
| 69 | |
---|
| 70 | if ($sort_by_time) { |
---|
| 71 | arsort($this->_timing_cumulative_times, SORT_NUMERIC); |
---|
| 72 | } |
---|
| 73 | |
---|
[172] | 74 | $this->_timing_cumulative_times['TOTAL'] = array_sum($this->_timing_cumulative_times); |
---|
[42] | 75 | |
---|
[92] | 76 | echo '<pre>'; |
---|
| 77 | foreach ($this->_timing_cumulative_times as $name => $time) { |
---|
| 78 | printf("\n%-{$name_len}s $this->time_format", $name, $time); |
---|
| 79 | } |
---|
| 80 | echo '</pre>'; |
---|
| 81 | } |
---|
[1] | 82 | } |
---|
| 83 | |
---|
| 84 | ?> |
---|