<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2001 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | [email protected] so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Sebastian Bergmann <[email protected]>               |
// +----------------------------------------------------------------------+
//
// $Id: Timer.php,v 1.8 2001/03/03 06:35:25 sbergmann Exp $
///**
 * Bench::Timer
 * 
 * Purpose:
 * 
 *     Timing Script Execution, Generating Profiling Information
 * 
 * Example:
 * 
 *     $timer = new Bench_Timer;
 * 
 *     $timer->start();
 *     $timer->setMarker('Marker 1');
 *     $timer->stop();
 * 
 *     $profiling = $timer->getProfiling();
 *     $runtime   = $timer->timeElapsed();
 * 
 * @author   Sebastian Bergmann <[email protected]>
 * @version  $Revision: 1.8 $
 * @access   public
 */
class Bench_Timer
{
    /**
     * Contains the ers
     *
     * @var    array
     * @access public
     */
    var $ers = array();    /**
     * Set "Start" er.
     *
     * @see    setMarker(), stop()
     * @access public
     */
    function start()
    {
        $this->setMarker('Start');
    }    /**
     * Set "Stop" er.
     *
     * @see    setMarker(), start()
     * @access public
     */
    function stop()
    {
        $this->setMarker('Stop');
    }    /**
     * Set er.
     *
     * @param  string  name of the er to be set
     * @see    start(), stop()
     * @access public
     */
    function setMarker($name)
    {
        $microtime = explode(' ', microtime());
        $this->ers[$name] = $microtime[1] . substr($microtime[0], 1);
    }    /**
     * Returns the time elapsed betweens two ers.
     *
     * @param  string  $start        start er, defaults to "Start"
     * @param  string  $end          end er, defaults to "Stop"
     * @return double  $time_elapsed time elapsed between $start and $end
     * @access public
     */
    function timeElapsed($start = 'Start', $end = 'Stop')
    {
        if (extension_loaded('bcmath')) {
            return bcsub($this->ers[$end], $this->ers[$start], 6);
        } else {
            return $this->ers[$end] - $this->ers[$start];
        }
    }    /**
     * Returns profiling information.
     *
     * $profiling[x]['name']  = name of er x
     * $profiling[x]['time']  = time index of er x
     * $profiling[x]['diff']  = execution time from er x-1 to this er x
     * $profiling[x]['total'] = total execution time up to er x
     *
     * @return array $profiling
     * @access public
     */
    function getProfiling()
    {
        $i = 0;
        $total = 0;
        $result = array();
        
        foreach ($this->ers as $er => $time) {
            if ($er == 'Start') {
                $diff = '-';
            } else {
                if (extension_loaded('bcmath')) {
                    $diff  = bcsub($time,  $temp, 6);
                    $total = bcadd($total, $diff, 6);
                } else {
                    $diff  = $time - $temp;
                    $total = $total + $diff;
                }
            }
            
            $result[$i]['name']  = $er;
            $result[$i]['time']  = $time;
            $result[$i]['diff']  = $diff;
            $result[$i]['total'] = $total;
            
            $temp = $time;
            $i++;
        }        return $result;
    }
}
?>

解决方案 »

  1.   

    * Example:
     * 
     *     $timer = new Bench_Timer;
     * 
     *     $timer->start();
     *     $timer->setMarker('Marker 1');
     *     $timer->stop();
     * 
     *     $profiling = $timer->getProfiling();
     *     $runtime   = $timer->timeElapsed();
      

  2.   

    给一个简单又实际一点的
    $start = microtime();
    ...
    mysql_query("SELECT * FROM table .....");
    这里加入你的查询语句
    ...
    $end = microtime();
    $starttime = explode(" ", $start);
    $endtime = explode(" ", $end);
    $exectime=$endtime[0]+$endtime[1]-$starttime[0]-$starttime[1];
    echo "执行时间为:" . $exectime;
      

  3.   

    function getmicrotime(){
        list($usec, $sec) = explode(" ",microtime());
        return ((float)$usec + (float)$sec);
    }
    $start = getmicrotime();
    ......
    $end = getmicrotime();
    echo "执行时间为:" ($end - $start);