Home  >  Article  >  Backend Development  >  php-timeit estimates the execution time of php functions

php-timeit estimates the execution time of php functions

WBOY
WBOYOriginal
2016-07-29 09:16:13884browse

 First of all, I used the Japanese VPS on hand to set up a Google proxy some time ago. The access speed is okay. I would like to share with you:

 Google gugeIf it doesn’t work, just call 119

 Google: guge119.com

Google Scholar: scholar.guge119.com

Sometimes when we optimize PHP performance, we need to know the execution time of a certain function. In Python, there is the timeit module. Is there a similar module in PHP?

So, I wrote a simple timeit function myself, as follows:

/**
 * Compute the delay to execute a function a number of time
 * @param $count    Number of time that the tests will execute the given function
 * @param $function        the function to test. Can be a string with parameters (ex: 'myfunc(123, 0, 342)') or a callback
 * @return float            Duration in seconds (as a float)
 */
function timeit($count, $function<span>) {
    if ($count <= 0<span>){
        echo "Error: count have to be more than zero"<span>;
        return -1<span>;
    }
    
    $nbargs = func_num_args<span>();
    if ($nbargs < 2<span>) {
        echo 'Error: No Funciton!'<span>;
        echo 'Usage:'<span>;
        echo "\ttimeit(count, 'function(param)')"<span>;
        echo "\te.g:timeit(100, 'function(0,2)')"<span>;
        return -1;                        // no function to time
<span>    }
    
    // Generate callback
    $func = func_get_arg(1<span>);
    $func_name = current(explode('(', $func<span>));
    if (!function_exists($func_name<span>)) {
        echo 'Error: Unknown Function'<span>;
        return -1;                    // can't test unknown function
<span>    }
    
    $str_cmd = ''<span>;
    $str_cmd .= '$start = microtime(true);'<span>;
    $str_cmd .= 'for($i=0; $i<'.$count.'; $i++) '.$func.';'<span>;
    $str_cmd .= '$end = microtime(true);'<span>;
    $str_cmd .= 'return ($end - $start);'<span>;
    
    return eval($str_cmd<span>);
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

Test the execution time of a root-finding algorithm I wrote and the system’s built-in root-finding function, as follows:

//取平方根
function sqrt_nd($num<span>){
    $value = $num<span>;
    while(abs($value*$value -$num) > 0.001<span>){
        $value = ($value + $num/$value)/2<span>;
    }
    return $value<span>;
}


print timeit(1000, 'sqrt_nd(5)'<span>);
print "\n"<span>;
print timeit(1000, 'sqrt(5)');</span></span></span></span></span></span>

The test results are as follows :

0.028280019760132
0.0041000843048096

 It can be seen that the built-in root function is more than 6 times faster than the custom root function~~

The above introduces how php-timeit estimates the execution time of PHP functions, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn