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

1

3

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.

Bowlder answered 24/2, 2015 at 18:48 Comment(0)
P
10
  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.

Peculiarize answered 24/2, 2015 at 18:59 Comment(2)
Just wondering if it makes senses... the 1st one should be a faster/smaller time being that it's for 'precision' right? does it make sense that the 2nd actually returns in my case a faster/smaller execution time? is that possible?Bowlder
I am not really sure, what you exactly mean. 1st code snippet is not useful at all for measuring execution time as both the values store timestamp value when server received request. Only difference is $_SERVER['REQUEST_TIME_FLOAT'] would store it in micro second precision, making it more accurate. I hope I answer your doubts.Peculiarize

© 2022 - 2024 — McMap. All rights reserved.