|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
This function benchmarks another one specified as first parameter using a callback. The example demostrates how to use callbacks, create lambda-style function and profile your code execution time.
You can use this function as an entry-point to write your own specific function/class for profiling your code.
benchmark.function.php
| <?php
/**
* @fn benchmark($function, $parameters = null, $executions = 1)
* @brief Gets the time taken for execute X function Y times
* @param callback $function Function to be called
* @param mixed $parameters Parameters passed to that function
* @param int $executions Number of times the function must be called
* @return PHP5: float Difference between first and last call
* @return PHP4: int Difference between first and last call
*/
function benchmark($function, $parameters = null, $executions = 1)
{
$start_time = null; /* Function call started at */
$end_time = null; /* Function call ended at */
$total_time = 0; /* Total time consumed by benchmark */
$c_executions = 0; /* # of times function was executed */
$mt_call = null; /* Internal microtime() callback func */
$mt_body = null; /* Internal microtime() callback body */
$cb_call = null; /* Internal callback used with $function */
/* The function will be inexistant so check for it */
if (false == function_exists($function))
trigger_error("Function \"$function\" does not exists.", E_USER_ERROR);
/*
* Checks that $executions is a positive integer. If not, fix it
* to 1.
*/
if (!is_int($executions) || $executions <= 0)
$executions = 1;
/*
* This check determines if the $parameters param is an array. In
* that case we must call call_user_func_array, otherwise the call
* is passed to call_user_func
*/
$cb_call = is_array($parameters) ? "call_user_func_array" : "call_user_func";
/*
* Since PHP >= 5.0.0 we can pass "true" as microtime() parameter
* to get the microtime as a float value.
*/
$mt_body = 'return version_compare("5", PHP_VERSION, "<") >= 0
? microtime(true) : microtime();';
$mt_call = create_function(null, $mt_body);
/* Gets the time when the first function call was made */
$start_time = call_user_func($mt_call);
for ($c_executions = 0; $c_executions < $executions; $c_executions++)
call_user_func_array($cb_call, array($function, $parameters));
/* Gets the time when the last function call was made */
$end_time = call_user_func($mt_call);
/* Now returns the difference between start and end time */
$total_time = $end_time - $start_time;
return $total_time;
}
?> | |
Usage Example
| <?php
require_once "benchmark.function.php";
/* Every function will be called 50000 times: */
$times = 50000;
/* For simplicity, an array to run 3 callbacks (first array entry) with their parameters (second array entries) */
$benchmark_fns = array("print_array" => array("one", "two", "three"),
"my_pow" => array(2, 10),
"count_until" => "1000000",
);
/* Iterates over the previous array to benchmark all function call times: */
foreach ($benchmark_fns as $function => $parameters) {
echo "Calling $function...<br />";
$time_taken = benchmark($function, $parameters, $times);
echo "Time taken to call ".$function." ".$times." times: $time_taken s.<br />";
}
/* Since there, some example functions to illustrate the benchmark */
function count_until($var)
{
if (!is_int($var))
return;
for ($temp = 0; $temp < $var; $temp++);
}
function print_array($my_array)
{
if (!is_array($my_array))
return;
foreach ($my_array as $key => $val)
echo "\$my_array[$key] => $val<br />";
}
function my_pow($base, $exponent)
{
$result = 1;
if (!is_numeric($base) || !is_numeric($exponent))
return 0;
while ($exponent-- > 1)
$result *= $base;
return $result;
}
?> | | |
|
| A time measuring and performance benchmarking class Categories : PHP, PHP Classes, Testing, Debugging, Date Time | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | PHP Tester - Lets you test php code from a browser. Categories : PHP, HTML and PHP, Testing | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Using $PHP_AUTH_USER and $PHP_AUTH_PW to authenticate. Categories : Authentication, PHP | | | very simple ftp class Categories : PHP, PHP Classes, FTP | | | PHP Paypal IPN Integration Class v1.0.0 Categories : PHP, PHP Classes, Payment Gateways | | | Function to remember password Categories : PHP, Authentication, Personalization and Membership | | | Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality. Categories : PHP, GD image library, Graphics | | | readline -- Reads a line Categories : PHP, PHP Functions, Readline | | | a function that builds an HTML select list from any mysql table. Categories : PHP, MySQL, HTML and PHP | | | Math operations on big numbers Categories : PHP, Math. | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | |
|
|
|