source: trunk/lib/ScriptTimer.inc.php

Last change on this file was 703, checked in by anonymous, 5 years ago

Apply rounding to ScriptTimer? getTime() output

File size: 2.9 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-2012 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
29    public $time_format = '%.4F';
30    protected $_timing_start_times = array();
31    protected $_timing_stop_times = array();
32    protected $_timing_cumulative_times = array();
33
34    public function start($name='default')
35    {
36        $this->_timing_start_times[$name] = explode(' ', microtime());
37    }
38
39    public function stop($name='default')
40    {
41        if (!isset($this->_timing_stop_times[$name]) || !isset($this->_timing_cumulative_times[$name])) {
42            $this->_timing_stop_times[$name] = null;
43            $this->_timing_cumulative_times[$name] = null;
44        }
45        $this->_timing_stop_times[$name] = explode(' ', microtime());
46        $this->_timing_cumulative_times[$name] += $this->getTime($name);
47    }
48
49    public function getTime($name='default')
50    {
51        if (!isset($this->_timing_start_times[$name])) {
52            return 0;
53        }
54        if (!isset($this->_timing_stop_times[$name])) {
55            $stop_time = explode(' ', microtime());
56        } else {
57            $stop_time = $this->_timing_stop_times[$name];
58        }
59        // Do the big numbers first so the small ones aren't lost.
60        $current = $stop_time[1] - $this->_timing_start_times[$name][1];
61        $current += $stop_time[0] - $this->_timing_start_times[$name][0];
62        return sprintf($this->time_format, $current);
63    }
64
65    public function printAll($sort_by_time=false)
66    {
67        $names = array_map('strlen', array_keys($this->_timing_start_times));
68        sort($names);
69        $name_len = end($names);
70
71        if ($sort_by_time) {
72           arsort($this->_timing_cumulative_times, SORT_NUMERIC);
73        }
74
75        $this->_timing_cumulative_times['TOTAL'] = array_sum($this->_timing_cumulative_times);
76
77        echo '<pre>';
78        foreach ($this->_timing_cumulative_times as $name => $time) {
79            printf("\n%-{$name_len}s $this->time_format", $name, $time);
80        }
81        echo '</pre>';
82    }
83}
Note: See TracBrowser for help on using the repository browser.