您好,欢迎访问一九零五行业门户网

php-timeit估计php函数的执行时间,php-timeitphp_PHP教程

php-timeit估计php函数的执行时间,php-timeitphp首先,前段时间利用手头的日本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 <= 0){ echo error: count have to be 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(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 0.001){ $value = ($value + $num/$value)/2; } return $value;}print timeit(1000, 'sqrt_nd(5)');print \n;print timeit(1000, 'sqrt(5)');
测试结果如下:
0.0282800197601320.0041000843048096
可见,内置求根函数比自定义的求根函数快了6倍多~~
http://www.bkjia.com/phpjc/1051322.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1051322.htmltecharticlephp-timeit估计php函数的执行时间,php-timeitphp 首先,前段时间利用手头的日本vps搭建了一个google代理,访问速度还行,分享给大家: 谷歌 g...
其它类似信息

推荐信息