Jump to content

[SOLVED] any way to show each function cost how much second?


zgkhoo

Recommended Posts

You can invest in an IDE with a "profiler" or use microtime()

 

<?php
$t1 = microtime(true);

myfunction();   // call function

$t2 = microtime(true);

echo $t2 - $t1;    // echo time taken

I use this class

 

/**
* This class is used to time program execution. This is particularly handy for
* performance trouble shooting.
* @package Global
* @author Tony Bibbs
*
*/
class timerobject {

    private $_starttime = '';
    private $_endtime = '';
    private $_elapsedtime = '';
    private $_percision = 2;

    // PUBLIC METHODS

    /**
    * Constructor
    *
    * This initializes the timerobject and sets the default
    * percision of results to two decimal places
    *
    */
    function timerobject()
    {
    }

    /**
    * Set percision on timer results
    *
    * This sets how many significant digits get
    * sent back when elapsedTime is called
    *
    * @param    int     $num_dec_places     Number of significant digits
    *
    */
    function setPrecision($num_dec_places)
    {
        $this->_percision = $num_dec_places;
    }

    /**
    * Starts the timer
    *
    */
    function startTimer()
    {
        $mtime = microtime();
        $mtime = explode(' ', $mtime);
        $mtime = $mtime[1] + $mtime[0];
        $this->_starttime = $mtime;
    }

    /**
    * Stops the timer
    *
    * @return   float   elapsed time to degree of percision specified
    *
    */
    function stopTimer()
    {
        $mtime = microtime();
        $mtime = explode(' ',$mtime);
        $mtime = $mtime[1] + $mtime[0];
        $this->_endtime = $mtime;
        $this->_setElapsedTime();

        // We are going to assume that when the timer is stopped
        // they will want the elapsed time immediately
        return $this->getElapsedTime();
    }

    /** 
    * Restarts the timer
    *
    * Same as starTimer excepts this clears everything out first
    *
    */
    function restart()
    {
        $this->_endtime = '';
        $this->_elapsedtime = '';
        
        $this->starTimer();      
    }

    /**
    * Gets the elapsed time
    *
    * This returns the elapsed time with the proper number of 
    * significant digits
    * 
    * updated by ben to add code so that the _endtime gets set
    *
    * @return   float   Elasped time in seconds formatted to degree of percision specified
    *
    */
    function getElapsedTime()
    {	
        $mtime = microtime();
        $mtime = explode(' ',$mtime);
        $mtime = $mtime[1] + $mtime[0];
        $this->_endtime = $mtime;
        $this->_setElapsedTime();
        return sprintf("%.{$this->_percision}f", $this->_elapsedtime);
    }

    // PRIVATE METHODS

    /**
    * Sets the elapsed time
    *
    * once stop timer is called this gets called to calculate
    * the elapsed time for later retrieval
    *
    * @access private
    */
    function _setElapsedTime()
    {
        $this->_elapsedtime = $this->_endtime - $this->_starttime;
    }

}

 

sample code

 

<?
$summarytimer = new timerobject();
$summarytimer->setPrecision(4);
$summarytimer->startTimer();
... run your function
echo $summarytimer->getElapsedTime();
... run another function
echo $summarytimer->stopTimer();
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.