我的网站中有一个大型 PHP 代码,我想知道处理的执行时间。我怎样才能做到这一点?
<?php
// large code
// large code
// large code
// print execution time here
?>
您可以microtime
用作PHP 代码的开始和结束:
<?php
$time_start = microtime(true);
sleep(1);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Process Time: {$time}";
// Process Time: 1.0000340938568
?>
从 PHP 5.4.0开始,不需要在开始时获取开始时间,$_SERVER
超全局数组已经有了它:
<?php
sleep(1);
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Process Time: {$time}";
// Process Time: 1.0061590671539
?>