Use microseconds to compute script Execution time examples in PHP _javascript tips

Source: Internet
Author: User
Tags php script

In PHP, most time formats are represented by a Unix timestamp, while the UNIX timestamp is the unit of measure time with a minimum of s (seconds). This is not accurate enough for some applications, so you can call Microtime () to return the current UNIX timestamp and subtle number. The prototype of the function is as follows:

Copy Code code as follows:

Mixed Microtime ([bool get_as_float]); Returns the current UNIX time stamp and subtle number

You can provide an optional Boolean parameter for the function, and if this argument is not supplied at the time of the call, this function returns a string in the format "msec sec". Where the SEC is the number of seconds from the Unix era to the present, and msec is a delicate 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 (), a floating-point number is returned. Before the decimal point or in the timestamp format, and after the decimal point, the subtle value is represented. Note, however, that the parameter get_as_float is added in the PHP5.0 version, so in the previous version of PHP5, you cannot directly request a floating-point number with this parameter. In the following example, the Microtime () function is invoked two times to calculate the time required to run the PHP script. The code looks like this:
Copy Code code as follows:

<?php
Life A class that calculates the running time of a script
Class timer{
Private $startTime = 0; time when the Save script starts executing (saved in microseconds)
Private $stopTime = 0; Time to save the script at the end of execution (in microseconds)

Call the Microsecond value at the beginning of the script to get the script start time
function Start () {
$this->starttime = Microtime (true); Assign the acquired time to member properties $starttime
}
At the end of the script the time microsecond value of the end of the script
function Stop () {
$this->stoptime = Microtime (true); Assigns the time that is obtained to member properties $stoptime
}
Returns the difference between two fetch times in the same script
function spent () {
4 5 into the remaining 4 digits returned
Return round (($this->stoptime-$this->starttime), 4);
}
}

$timer = new timer ();
$timer->start (); This method is called when the script file starts executing
Usleep (1000); The subject content of the script, where you can hibernate for a millisecond example
$timer->stop (); Call this method at the end of the script file

echo "<b>" when executing the script. $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 execution of the script to get the time when the script starts executing. 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. You can then obtain the time required to run the script by accessing the spent () method in the class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.