source: tags/2.1.5/lib/ScriptTimer.inc.php

Last change on this file was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 2.7 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2010 Strangecode, LLC
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/**
24 * ScriptTimer.inc.php
25 */
26class ScriptTimer {
27
28    var $time_format = '%.3f';
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    {
35        $this->_timing_start_times[$name] = explode(' ', microtime());
36    }
37   
38    function stop($name='default')
39    {
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        }
44        $this->_timing_stop_times[$name] = explode(' ', microtime());
45        $this->_timing_cumulative_times[$name] += $this->getTime($name);
46    }
47   
48    function getTime($name='default')
49    {
50        if (!isset($this->_timing_start_times[$name])) {
51            return 0;
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];
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       
74        $this->_timing_cumulative_times['TOTAL'] = array_sum($this->_timing_cumulative_times);
75
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    }
82}
83
84?>
Note: See TracBrowser for help on using the repository browser.