Measuring the time of PHP scripts - Using $_SERVER['REQUEST_TIME']

ぃ、小莉子 提交于 2019-12-02 02:27:49

问题


Are this methods a reliable way to measure a script:

$time = ($_SERVER['REQUEST_TIME_FLOAT'] - $_SERVER['REQUEST_TIME']);

or

$time = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);

Which one should be used?

And what's the difference of each one?

They return very different measurements.


回答1:


  1. $time = ($_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.

  1. $time = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);

I guess this is what should be used at the end of the PHP script and I think you know why.

Also keep in mind $_SERVER['REQUEST_TIME_FLOAT'] is available since PHP 5.4.0.



来源:https://stackoverflow.com/questions/28703822/measuring-the-time-of-php-scripts-using-serverrequest-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!