VB icon

Function Profiler

Email
Submitted on: 1/1/2015 11:24:00 PM
By: Michael Bailey (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: PHP 4.0
Views: 2480
 
     If your scripts are running too slow, it may be that you have a function that's taking too long to run or that gets called too often. This function profiler watches the all of the function calls and records how much time is spent in each function. All of the documentation is included at the top of the source code.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Function Profiler
// Description:If your scripts are running too slow, it may be that you have a function that's taking too long to run or that gets called too often. This function profiler watches the all of the function calls and records how much time is spent in each function. All of the documentation is included at the top of the source code.
// By: Michael Bailey (from psc cd)
//**************************************

<?
/**
* The best way to use this script is to place
* it in a separate file, say: watcher.php.
* At the top of the file you want to profile,
* include watcher.php and follow the include
* with the statement:
* declare(ticks=1);
*
* At the end of the script, call watch_function_output()
* to see the results. The value returned in
* the output for each function call is the
* number of system ticks spent in each given
* function/method. These ticks relate to
* the number of lower-level operations the
* PHP engine must perform. For more information,
* check the php.net documentation under
* declare() or contact me:
*Michael Bailey "jinxidoru@byu.net"
*
* You are welcome to use this code to any end,
* be it for good or evil. Though, if it be for
* evil, I'd be interested in knowing what you're
* doing for the sake of curiousity.
*/
// tick function for watching functions
// if $ret_funcs is true, a tick is not registered,
// but instead, the function list is returned.
function watch_functions_tick($ret_funcs=false) {
 static $funcs = array();
 // do tick
 if (!$ret_funcs) {
 
// get the current function from debug_backtrace
$trace = debug_backtrace();
$trace = $trace[1];
$func = $trace['function'];
if ($trace['class']) $func = $trace['class'].$trace['type'].$func;
// as long as the given function is a valid one, add it to the array
if ($func !== 'watch_functions_tick' && $func != 'unknown' && $func) {
 $funcs[$func]++;
 $funcs['__TICK_COUNT__']++;
}
 
 // return the function array
 } else {
return $funcs;
 }
}
// output the function list with the number of ticks each
// function received as well as the percentage of the total
// ticks it represents 
function watch_functions_output() {
 
 // retrieve the function list
 $funcs = watch_functions_tick(true);
 $tick_count = $funcs['__TICK_COUNT__'];
 arsort($funcs);
 
 // add the percentage onto each function entry
 foreach ($funcs AS $k => $v) {
$funcs[$k] = sprintf('%d (%.2f%%)', $funcs[$k], $funcs[$k]*100/$tick_count);
 }
 
 // display the function list
 print "<pre>";
 print_r($funcs);
 print "</pre>";
}
// start the ticking
register_tick_function('watch_functions_tick');
?>


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.