程序在运行时, 遇到莫名奇妙运行很久, 但又不知道是什么问题的时后, 就用此 function 来将每个运行的时间印出来, 将可轻易找出问题所在.
function getMicrotime()
{
list($usec, $sec) = explode(' ', microtime());
return ((double)$usec + (double)$sec);
}?>
使用方法:(以抓 mysql_connect() 时间为例)
$start = getMicrotime();
mysql_connect('localhost', 'id', 'password');
$end = getMicrotime();
$time = $end - $start;
echo "mysql_connect 运行时间: $time";
?>
注: PHP5 以后, 可以采用下述作法:
$time_start = microtime(true);
// Sleep for a while
usleep(100);
$time_end = microtime(true);
$time = $time_end - $time_start;
?>
或
$time_start = microtime(true);
// Sleep for a while
usleep(100);
$time = microtime(true) - $time_start;
?>