Home > Backend Development > PHP Tutorial > php-timeit estimates the execution time of php functions, php-timeitphp_PHP tutorial

php-timeit estimates the execution time of php functions, php-timeitphp_PHP tutorial

WBOY
Release: 2016-07-13 09:44:05
Original
906 people have browsed it

php-timeit estimates the execution time of the php function, php-timeitphp

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

If Google guge doesn’t work, 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>
Copy after login

Test the execution time of a root-finding algorithm you 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></span>
Copy after login

The test results are as follows:

0.028280019760132
0.0041000843048096
Copy after login

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1051322.htmlTechArticlephp-timeit estimates the execution time of the php function, php-timeitphp First of all, I built it some time ago using the Japanese VPS on hand. A google proxy, the access speed is okay, share it with everyone: Google g...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template