This article demonstrates how to track the execution time of a script in PHP.

You can use the in-built PHP function microtime() to track the script execution time in PHP. It returns the current time in seconds since the Unix epoch, accurate to the nearest microsecond. If the second parameter is set to true, then it returns a float instead of a string in “msec sec” format.

 
You can invoke the microtime() function at the start of the script and call microtime() again immediately after the script execution. Then the difference between the return values of these two calls is the elapsed time in seconds. Note that the returned time is the wall-clock time and not the CPU execution time. In other words, the elapsed time measured with microtime() might not be accurate and can depend on external factors.

Download  Run Code

 
Alternatively, you can use REQUEST_TIME from the $_SERVER superglobal array to get the timestamp of the start of the request. To get the timestamp with microsecond precision, consider using REQUEST_TIME_FLOAT. This eliminates the need for microtime() call at the start of your script. The following solution shows how to use REQUEST_TIME_FLOAT to get the elapsed time in seconds from the beginning of the script, with microsecond precision.

Download  Run Code

That’s all about tracking the execution time of a script in PHP.