/ Published in: PHP
                    
                                        
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
<?php
/*
* Function: benchmark
* Author: Michael Bailey <[email protected]>
* Date: 5 Dec 2002
* License: GPL (General Public License)
* Purpose: This function will run the line of code
* sent as the first parameter and return
* the time it took to complete execution.
* If a second parameter is passed, the
* function will run the code the specified
* number of times, taking the average. This
* will return a better approximation. The
* last parameter allows you to send code to
* be executed between iterations, in case
* something needs to be reset (ie. global
* variables, etc...). The function returns
* the number of seconds
* with a bunch of decimal places.
*/
function benchmark($code, $iter = 1, $reset_code = null)
{
# Determine the overhead in calling the eval function
# You might want to to this multiple times to get better
# precision.
$overhead = ($end_ms+$end_s) - ($start_ms+$start_s);
# Execute the code the specified number of times
for ($i=0; $i<$iter; $i++) {
$time += ($end_ms+$end_s) - ($start_ms+$start_s);
# If reset code was specified, evaluate it
}
# Return the average speed subtracted by
# the overhead of calling eval()
return ($time/$iter) - $overhead;
}
?>
URL: http://www.codewalkers.com/c/a/Miscellaneous-Code/Quick-n-Easy-Benchmark/
Comments
 Subscribe to comments
                    Subscribe to comments
                
                