Home  >  Article  >  Web Front-end  >  Example of using microseconds to calculate script execution time in PHP_javascript skills

Example of using microseconds to calculate script execution time in PHP_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:30:491915browse

In PHP, most time formats are expressed in UNIX timestamps, and UNIX timestamps are based on s (seconds) as the smallest unit of measurement of time. This is not precise enough for some applications, so microtime() can be called to return the current UNIX timestamp and number of microseconds. The prototype of this function is as follows:

Copy code The code is as follows:

mixed microtime([bool get_as_float]); //Return the current UNIX timestamp and microsecond

You can provide an optional Boolean parameter to this function. If this parameter is not provided when calling, this function returns a string in the format of "msec sec". Where sec is the number of seconds since the UNIX epoch and msec is the micropart, both parts of the string are returned in seconds. If the get_as_float argument is given and its value is equivalent to TRUE, microtime() will return a floating point number. Before the decimal point, it is still expressed in timestamp format, while after the decimal point, it represents the subtle value. However, it should be noted that the parameter get_as_float was newly added in the PHP5.0 version, so in versions prior to PHP5, this parameter cannot be used to directly request a floating point number. In the following example, the time required to run a PHP script is calculated by calling the microtime() function twice. The code looks like this:
Copy code The code is as follows:

// Create a class that calculates script running time
class Timer{
private $startTime = 0; //Save the time when the script starts executing (saved in microseconds)
private $stopTime = 0; //Save the time when the script ends execution (save in microseconds)

//Call at the beginning of the script to get the microsecond value of the script start time
function start(){
$this->startTime = microtime(true); //Assign the obtained time to the member attribute $startTime
}
//At the end of the script, use the microsecond value of the script end time
function stop(){
$this->stopTime = microtime(true); //Assign the obtained time to the member attribute $stopTime
}
//Return the difference between the two acquisition times in the same script
function spent(){
//After calculation, round 4 to 5, keep 4 digits and return
return round(($this->stopTime-$this->startTime),4);
}
}

$timer= new Timer();
$timer->start(); //Call this method when the script file starts executing
usleep(1000); //Theme content of the script, here you can sleep for one millisecond as an example
$timer->stop(); //Call this method at the end of the script file

echo "It took".$timer->spent()."";

?>

In the above script, declare a class Timer used to calculate the script execution time. You need to call the start() method in this class at the beginning of script execution to get the time when the script starts executing. And call the stop() method in this class at the end of script execution to get the time when the script ends. Then by accessing the spend() method in this class, you can get the time required to run the script.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn