Important alert: (current site time 7/15/2013 10:19:22 AM EDT)
 

VB icon

Function Profiler

Email
Submitted on: 7/1/2004 5:52:23 PM
By: Michael Bailey  
Level: Intermediate
User Rating: By 2 Users
Compatibility: PHP 4.0
Views: 7564
author picture
(About the author)
 
     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!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// 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
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1404&lngWId=8//for details.//**************************************

<?
/**
* 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

8/3/2004 7:25:03 AMRickard Sjöquist

Good work! Keep it up!
(If this comment was disrespectful, please report it.)

 
2/19/2005 2:34:24 PM

There is a missing 's':

* At the end of the script, call watch_function_output()

Should be
* At the end of the script, call watch_functions_output()

(If this comment was disrespectful, please report it.)

 
2/19/2005 2:37:10 PM

It is important to note that this functionality probably significantly impacts the performance of your system. Use it for debug only.
(If this comment was disrespectful, please report it.)

 

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.