网上不少误导信息,实际上这个答案在PHP源码中的Zend文件夹下bench.php是有的
在此纠正下网络上复制粘贴造成的错误。希望后来人少踩点坑。
function getmicrotime() { $t = gettimeofday(); return ($t['sec'] + $t['usec'] / 1000000); } function start_test() { ob_start(); return getmicrotime(); } function end_test($start, $name) { global $total; $end = getmicrotime(); ob_end_clean(); $total += $end-$start; $num = number_format($end-$start,3); $pad = str_repeat(" ", 24-strlen($name)-strlen($num)); echo $name.$pad.$num."\n"; ob_start(); return getmicrotime(); }
调用
$t0 = $t = start_test(); simple(); $t = end_test($t, "simple");