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
RevLine 
[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/>
[396]5 * Copyright 2001-2012 Strangecode, LLC
[703]6 *
[362]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.
[703]13 *
[362]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.
[703]18 *
[362]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 */
[502]26class ScriptTimer
27{
[1]28
[703]29    public $time_format = '%.4F';
[484]30    protected $_timing_start_times = array();
31    protected $_timing_stop_times = array();
32    protected $_timing_cumulative_times = array();
[703]33
[479]34    public function start($name='default')
35    {
[1]36        $this->_timing_start_times[$name] = explode(' ', microtime());
[479]37    }
[703]38
[479]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        }
[1]45        $this->_timing_stop_times[$name] = explode(' ', microtime());
[92]46        $this->_timing_cumulative_times[$name] += $this->getTime($name);
[479]47    }
[703]48
[479]49    public function getTime($name='default')
50    {
[1]51        if (!isset($this->_timing_start_times[$name])) {
[479]52            return 0;
[1]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];
[703]62        return sprintf($this->time_format, $current);
[479]63    }
[703]64
[479]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);
[703]70
[479]71        if ($sort_by_time) {
72           arsort($this->_timing_cumulative_times, SORT_NUMERIC);
73        }
[703]74
[479]75        $this->_timing_cumulative_times['TOTAL'] = array_sum($this->_timing_cumulative_times);
[42]76
[92]77        echo '<pre>';
[479]78        foreach ($this->_timing_cumulative_times as $name => $time) {
79            printf("\n%-{$name_len}s $this->time_format", $name, $time);
80        }
[92]81        echo '</pre>';
[479]82    }
[1]83}
Note: See TracBrowser for help on using the repository browser.