测量PHP脚本的时间-使用$ _SERVER ['REQUEST_TIME'] [英] Measuring the time of PHP scripts - Using $_SERVER['REQUEST_TIME']
问题描述
这种方法是衡量脚本的可靠方法吗?
Are this methods a reliable way to measure a script:
$time = ($_SERVER['REQUEST_TIME_FLOAT'] - $_SERVER['REQUEST_TIME']);
或
$time = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);
应该使用哪个?
每个人有什么区别?
它们返回非常不同的测量值.
They return very different measurements.
推荐答案
-
$time = ($_SERVER['REQUEST_TIME_FLOAT'] - $_SERVER['REQUEST_TIME']);
这将永远不会给您PHP脚本的执行时间.因为这两个值都用于存储请求开始.区别在于,$_SERVER['REQUEST_TIME_FLOAT']
更精确,并且以微秒精度存储时间值,而$_SERVER['REQUEST_TIME']
以秒为单位.
This will never give you execution time of you PHP script. Because both the values are used for storing start of request. The difference is, $_SERVER['REQUEST_TIME_FLOAT']
is more precise and stores time value with microsecond precision, while $_SERVER['REQUEST_TIME']
in seconds.
-
$time = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);
我想这是在PHP脚本末尾应使用的方式,我想您知道原因.
I guess this is what should be used at the end of the PHP script and I think you know why.
还请记住,自PHP 5.4.0起,可以使用$ _SERVER ['REQUEST_TIME_FLOAT'].
Also keep in mind $_SERVER['REQUEST_TIME_FLOAT'] is available since PHP 5.4.0.
这篇关于测量PHP脚本的时间-使用$ _SERVER ['REQUEST_TIME']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!