Example of using microseconds to calculate script execution time in PHP

  • 2020-03-30 04:21:35
  • OfStack

In PHP, most time formats are represented by UNIX timestamps, which are the smallest unit of time measured in s(seconds). This is not precise enough for some applications, so you can call microtime() to return the current UNIX timestamp and the subtlety number. The prototype of this function is as follows:


mixed microtime([bool get_as_float]); //Returns the current UNIX timestamp and the subtlety number

You can provide an optional Boolean argument to the function, and if you do not provide this argument when called, the function returns a string in the form of "msec SEC SEC". Where secant is the number of seconds since the UNIX epoch to the present, and msec is the subtle part, both parts of the string are returned in seconds. If the get_as_float parameter is given and its value is equivalent to TRUE, microtime() returns a floating point number. It is still represented in timestamp format before the decimal point and behind the decimal point is a subtle value. Note, however, that the parameter get_as_float was added in PHP5.0, so in previous versions of PHP5, you could not directly request a floating point number with this parameter. In the following example, calculate the time it takes to run the PHP script by calling the microtime() function twice. The code is as follows:

<?php
//
is a class that calculates the runtime of a script class Timer{
private $startTime = 0; //Save the time (in microseconds) when the script starts executing
private $stopTime = 0; //Save the time at the end of script execution (in microseconds)
 
//Call at the beginning of the script to get the value of the script start time in microseconds
function start(){
$this->startTime = microtime(true); //Assign the obtained time to the member property $startTime
}
//At the end of the script, the time in microseconds when the script ends is
function stop(){
$this->stopTime = microtime(true); //Assign the obtained time to the member attribute $stopTime
}
//Returns the difference between two fetch times in the same script
function spent(){
//





return round(($this->stopTime-$this->startTime),4);
}
}
 
$timer= new Timer();
$timer->start(); //This method is called
at the beginning of the execution of the script file usleep(1000); //The script's topic content, where you can sleep for a millisecond as an example
$timer->stop(); //This method is called at the end of the script file
 
echo " When the script is executed <b>".$timer->spent()."</b>";
 
?>

In the above script, declare a class Timer for calculating the execution time of the script. You need to call the start() method in the class at the beginning of the script execution to get the time when the script starts. And call the stop() method in the class at the end of the script execution to get the time at the end of the script run. The time required to run the script is then obtained by accessing the spent() method in the class.


Related articles: