我的网站上有一个很大的PHP代码,我想知道处理的执行时间.我怎样才能做到这一点?
<?php
// large code
// large code
// large code
// print execution time here
?>
Run Code Online (Sandbox Code Playgroud)
小智 82
您可以将其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
?>
Run Code Online (Sandbox Code Playgroud)
从PHP 5.4.0开始,没有必要在开始时获得开始时间,$_SERVER超全局数组已经拥有它:
<?php
sleep(1);
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Process Time: {$time}";
// Process Time: 1.0061590671539
?>
Run Code Online (Sandbox Code Playgroud)