php-timeit估计php函数的执行时间
程序员文章站
2022-04-04 15:53:01
...
首先,前段时间利用手头的日本VPS搭建了一个google代理,访问速度还行,分享给大家:
谷歌guge不行了,就打119
谷歌:guge119.com
谷歌学术:scholar.guge119.com
有时候我们在PHP性能优化的时候,需要知道某个函数的执行时间,在Python中,有timeit模块,在PHP中不知道有没有类似的模块?
于是,我自己写了一个简单的timeit函数,如下:
/**
* 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) {
if ($count ){
echo "Error: count have to be more than zero";
return -1;
}
$nbargs = func_num_args();
if ($nbargs ) {
echo 'Error: No Funciton!';
echo 'Usage:';
echo "\ttimeit(count, 'function(param)')";
echo "\te.g:timeit(100, '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
}
$str_cmd = '';
$str_cmd .= '$start = microtime(true);';
$str_cmd .= 'for($i=0; $i;
$str_cmd .= '$end = microtime(true);';
$str_cmd .= 'return ($end - $start);';
return eval($str_cmd);
}
上一篇: MySQL远程数据导出导入_MySQL