Using microsecond computing script execution time in PHP example _ javascript tips-js tutorial

Source: Internet
Author: User
Tags php example
This article mainly introduces the example of using the microsecond computing script execution time in PHP. This article first explains some knowledge about the microtime function and then provides a class for calculating the script running time, for more information, see PHP. In most time formats, UNIX timestamps are used, while UNIX timestamps are measured in seconds. This is not accurate enough for some applications, so microtime () can be called to return the current UNIX Timestamp and number of nuances. The function is prototype as follows:

The Code is as follows:


Mixed microtime ([bool get_as_float]); // returns the current UNIX Timestamp and number of nuances.


This function can provide an optional Boolean parameter. If this parameter is not provided during call, this function returns a string in the format of "msec sec. The sec is the number of seconds from the UNIX epoch to the present, while the 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. The decimal point is also expressed in the timestamp format, while the decimal point is a subtle value. Note that the get_as_float parameter is newly added in PHP5.0. Therefore, in versions earlier than PHP5, you cannot directly use this parameter to request a floating point number. In the following example, the microtime () function is called twice to calculate the time required to run the PHP script. The Code is as follows:

The Code is as follows:


<? Php
// A class for calculating the script running time
Class Timer {
Private $ startTime = 0; // The time when the script is saved for execution (saved in microseconds)
Private $ stopTime = 0; // save the time when the script ends (saved in microseconds)

// Call at the beginning of the script to obtain the microsecond value of the script Start Time
Function start (){
$ This-> startTime = microtime (true); // assign the obtained time to the member attribute $ startTime
}
// The microsecond Of the script end time when the terminal ends with the script
Function stop (){
$ This-> stopTime = microtime (true); // assign the obtained time to the member attribute $ stopTime
}
// Returns the difference between the two retrieved times in the same script
Function spent (){
// After calculation, four homes, five homes, four Reserved bits are returned.
Return round ($ this-> stopTime-$ this-> startTime), 4 );
}
}

$ Timer = new Timer ();
$ Timer-> start (); // this method is called when the script file starts to be executed.
Usleep (1000); // The subject content of the script, which can be sleep for one millisecond.
$ Timer-> stop (); // call this method at the end of the script file

Echo "when executing this script". $ Timer-> spent ()."";

?>

In the preceding script, declare a Timer class used to calculate the script execution time. You must call the start () method in the class at the beginning of script execution to obtain the time when the script starts to be executed. Call the stop () method in the class at the end of the script execution to obtain the end time of the script. Access the spent () method in the class to obtain the time required to run the script.

Related Article

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.