php 如何找到php执行时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17035859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find php execution time?
提问by
I have a large PHP code in my website, I want to know the execution timeof processing. How can I do this?
我的网站中有大量的 PHP 代码,我想知道处理的执行时间。我怎样才能做到这一点?
<?php
// large code
// large code
// large code
// print execution time here
?>
回答by
You can use microtime
as the startand endof your PHP code:
您可以使用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
?>
As of PHP 5.4.0, there is no need to get start time at the beginning,
the $_SERVER
superglobal array already has it:
从 PHP 5.4.0开始,不需要获取开始时间,$_SERVER
超全局数组已经有了:
<?php
sleep(1);
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Process Time: {$time}";
// Process Time: 1.0061590671539
?>