Php-timeit estimate the execution time of the PHP function, php-timeitphp
First, the previous period of time using the Japanese VPS at hand to build a Google agent, access speed is OK, share to everyone:
Google Guge , just hit 119
Google:guge119. com
Google Scholar: Scholar. guge119. com
Sometimes when we are in PHP performance optimization, we need to know the execution time of a function, in Python, there is a Timeit module, in PHP do not know that there is no similar module?
So, I wrote a simple Timeit function, as follows:
/** * Compute The delay to execute a function a number of time * @param $count number of time that the tests would EXECU Te the given function * @param $function the function to test. Can is a string with parameters (ex: ' MyFunc (123, 0, 342) ') or a callback * @return float Duration in seconds ( As a float) */function Timeit ($count, $function) {if ($count <= 0) {echo "Error:count has the to is more than zero" ; Return-1 ; } $nbargs = Func_num_args (); if ($nbargs < 2 ) {echo ' Error:no funciton! ' ; Echo ' Usage: ' ; Echo ' \ttimeit (count, ' function (param) ') ' ; Echo ' \te.g:timeit (' function (0,2) ') "; return-1; No function to time }//Generate callback $func = Func_get_arg (1 ), $func _name = current (Explode (' (', $func )); if (!function_exists ($func _name )) {echo ' Error:unknown function ' ; return-1;//Can ' t test Unknown function< span>} $str _cmd = ' ; $str _cmd. = ' $start = Microtime (true); ' ; $str _cmd. = ' for ($i =0; $i < '. $count. '; $i + +) '. $func. '; ' ; $str _cmd. = ' $end = Microtime (true); ' ; $str _cmd. = ' Return ($end-$start); ' , return eval ($str _cmd ),
Test yourself to write a root algorithm and the system built-in root function execution time, as follows:
Take the square root function sqrt_nd ($num) { $value = $num; while (ABS ($value * $value-$num) > 0.001) { $value = ($value + $num/$value)/2; } return $value;} Print Timeit (+, ' sqrt_nd (5) ');p rint "\ n";p rint Timeit (+, ' sqrt (5) ');
The test results are as follows:
0.0282800197601320.0041000843048096
It can be seen that the built-in root function is more than 6 times times faster than the custom root function ~ ~
http://www.bkjia.com/PHPjc/1051322.html www.bkjia.com true http://www.bkjia.com/PHPjc/1051322.html techarticle Php-timeit estimate the execution time of PHP functions, php-timeitphp First, the previous time using the Japanese VPS at hand to build a Google agent, access speed is OK, share to everyone: Google G ...