zgkhoo Posted November 24, 2007 Share Posted November 24, 2007 any way/code to show each function use how much time in second? thanks Link to comment https://forums.phpfreaks.com/topic/78658-solved-any-way-to-show-each-function-cost-how-much-second/ Share on other sites More sharing options...
zgkhoo Posted November 24, 2007 Author Share Posted November 24, 2007 for debugging purpose.. thanks in advance Link to comment https://forums.phpfreaks.com/topic/78658-solved-any-way-to-show-each-function-cost-how-much-second/#findComment-398013 Share on other sites More sharing options...
Barand Posted November 24, 2007 Share Posted November 24, 2007 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 Link to comment https://forums.phpfreaks.com/topic/78658-solved-any-way-to-show-each-function-cost-how-much-second/#findComment-398026 Share on other sites More sharing options...
BenInBlack Posted November 24, 2007 Share Posted November 24, 2007 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(); ?> Link to comment https://forums.phpfreaks.com/topic/78658-solved-any-way-to-show-each-function-cost-how-much-second/#findComment-398030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.