source: trunk/lib/ScriptTimer.inc.php @ 248

Last change on this file since 248 was 248, checked in by quinn, 17 years ago

Added multibyte charset function to App. Minor fixes.

File size: 1.9 KB
Line 
1<?php
2/**
3 * ScriptTimer.inc.php
4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
5 */
6class ScriptTimer {
7
8    var $time_format = '%.3f';
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        if (!isset($this->_timing_stop_times[$name]) || !isset($this->_timing_cumulative_times[$name])) {
21            $this->_timing_stop_times[$name] = null;
22            $this->_timing_cumulative_times[$name] = null;
23        }
24        $this->_timing_stop_times[$name] = explode(' ', microtime());
25        $this->_timing_cumulative_times[$name] += $this->getTime($name);
26    }
27   
28    function getTime($name='default')
29    {
30        if (!isset($this->_timing_start_times[$name])) {
31            return 0;
32        }
33        if (!isset($this->_timing_stop_times[$name])) {
34            $stop_time = explode(' ', microtime());
35        } else {
36            $stop_time = $this->_timing_stop_times[$name];
37        }
38        // Do the big numbers first so the small ones aren't lost.
39        $current = $stop_time[1] - $this->_timing_start_times[$name][1];
40        $current += $stop_time[0] - $this->_timing_start_times[$name][0];
41        return $current;
42    }
43   
44    function printAll($sort_by_time=false)
45    {
46        $names = array_map('strlen', array_keys($this->_timing_start_times));
47        sort($names);
48        $name_len = end($names);
49       
50        if ($sort_by_time) {
51           arsort($this->_timing_cumulative_times, SORT_NUMERIC);
52        }
53       
54        $this->_timing_cumulative_times['TOTAL'] = array_sum($this->_timing_cumulative_times);
55
56        echo '<pre>';
57        foreach ($this->_timing_cumulative_times as $name => $time) {
58            printf("\n%-{$name_len}s $this->time_format", $name, $time);
59        }
60        echo '</pre>';
61    }
62}
63
64?>
Note: See TracBrowser for help on using the repository browser.