blog
blog copied to clipboard
PHP性能检测里需要用到的获取毫秒数的函数(方便自己记录一下)
/**
* 程序运行耗时检测(单位:ms)
*
* @return int
*/
function get_time_cost_ms() {
static $last_time = 0;
$current_time = (int)(microtime(true)*1000);
$delta_time = $last_time == 0 ? 0 : ($current_time - $last_time);
$last_time = $current_time;
return $delta_time;
}
使用:在关键代码的前后调用一下该函数并把返回值打印出来,即可分析耗时! 比如:
...
echo 'step 1: ' .get_time_cost_ms();
$result = get_list(); // 想要监控的关键行
echo 'step 1.1: '.get_time_cost_ms();
...