1<?php
2/*
3 *  $Id: 085b1a92f765375e97d2c09c7569ca5747a44634 $
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information please see
19 * <http://phing.info>.
20 */
21
22
23/**
24 * This class can be used to obtain the execution time of all of the scripts
25 * that are executed in the process of building a page.
26 *
27 * Example:
28 * To be done before any scripts execute:
29 *
30 * $Timer = new Timer;
31 * $Timer->Start_Timer();
32 *
33 * To be done after all scripts have executed:
34 *
35 * $timer->Stop_Timer();
36 * $timer->Get_Elapsed_Time(int number_of_places);
37 *
38 * @author    Charles Killian
39 * @author    Hans Lellelid <hans@xmpl.org>
40 * @package    phing.system.util
41 * @version    $Id$
42 */
43class Timer {
44
45    /** start time */
46    protected $stime;
47
48    /** end time */
49    protected $etime;
50
51    /**
52     * This function sets the class variable $stime to the current time in
53     * microseconds.
54     * @return void
55     */
56    public function start() {
57        $this->stime = $this->getMicrotime();
58    }
59
60    /**
61     * This function sets the class variable $etime to the current time in
62     * microseconds.
63     * @return void
64     */
65    function stop() {
66        $this->etime = $this->getMicrotime();
67    }
68
69    /**
70     * This function returns the elapsed time in seconds.
71     *
72     * Call start_time() at the beginning of script execution and end_time() at
73     * the end of script execution.  Then, call elapsed_time() to obtain the
74     * difference between start_time() and end_time().
75     *
76     * @param    $places  decimal place precision of elapsed time (default is 5)
77     * @return string Properly formatted time.
78     */
79    function getElapsedTime($places=5) {
80        $etime = $this->etime - $this->stime;
81        $format = "%0.".$places."f";
82        return (sprintf ($format, $etime));
83    }
84
85    /**
86     * This function returns the current time in microseconds.
87     *
88     * @author    Everett Michaud, Zend.com
89     * @return    current time in microseconds
90     * @access    private
91     */
92    function getMicrotime() {
93        list($usec, $sec) = explode(" ", microtime());
94        return ((float)$usec + (float)$sec);
95    }
96}
97