WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Simple PHP function benchmark
Categories : PHP, Testing
Víctor Román Archidona
Date : Jun 05th 2008
Grade : 4 of 5 (graded 3 times)
Viewed : 5713
File : 4805.php
Images : No Images for this code example.
Search : More code by Víctor Román Archidona
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
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;
}
?>



Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
A time measuring and performance benchmarking class
Categories : PHP, PHP Classes, Testing, Debugging, Date Time
PHP Tester - Lets you test php code from a browser (modification).
Categories : PHP, HTML and PHP, Testing
Selecting a random line from a text file
Categories : PHP, Filesystem, Arrays, Beginner Guides
function_exists -- Return true if the given function has been defined
Categories : PHP, PHP Functions, Functions
Functions used to define a schedule of holidays. Can define non-fixed holidays (eg. 3rd sunday of June).
Categories : Calendar, Date Time, PHP
Class to Create protected URLs
Categories : PHP, PHP Classes, URLs
Is there any way to test that the $result has null values or not without reading the field values in the results in postgre?
Categories : PostgreSQL, PHP, Databases
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
Pageinfo: Array containing page URI, page query string (parameters), request method (GET or POST) and the complete URI
Categories : Variables, PHP Options and Info, Arrays, URLs, PHP
Classic guest book made with PHP and Flash
Categories : PHP, Flash, Java Script
getting the name of the current script and query string
Categories : PHP, Global Variables, Variables, URLs
How to get the exit code of a function ran by system().
Categories : PHP
Zephyr: AJAX Based Framework for PHP5 Developers
Categories : PHP, AJAX, Frameworks, Java Script, Web Applications
GetImageSize -- Get the size of a GIF, JPEG, PNG or SWF image
Categories : PHP, PHP Functions, Graphics