Simple example of php computing program running time _ PHP Tutorial

Source: Internet
Author: User
A simple example of php computing program running time is provided. First, let's analyze the principle. if you want to get the running time, you can define a variable at the beginning of the program to write down the current time. Then, after the program is running, let's first analyze the principle, to get the program running time, you can define a variable at the beginning of the program to write down the current time, and record the current time after the program runs successfully, the difference between the two is the time it takes for the program to run.

Here we will introduce the microtime () function. microtime () does not use much, but we cannot help but find this function. it returns the current Unix timestamp and the number of microseconds. For example, echo microtime (); will return 0.08845800 1376983061. Therefore, you can use the explode function to split it into an array with spaces. in this case, $ starttime [0] = 0.08845800 (microseconds ), $ starttime [1] = 1376983061 (current number of seconds, equivalent to the result obtained by time ).

Sample code:

The code is as follows:


// Program running time
$ Starttime = explode ('', microtime ());
Echo microtime ();

/* · The following code Area ·········*/
For ($ I = 0; I I <1000000; $ I ++ ){
$ I;
}
/* · The above is the code Area ·········*/

// Program running time
$ Endtime = explode ('', microtime ());
$ Thistime = $ endtime [0] + $ endtime [1]-($ starttime [0] + $ starttime [1]);
$ Thistime = round ($ thistime, 3 );
Echo "execution time of this webpage:". $ thistime. "seconds. ". Time ();
?>


At last, the time is reduced, and the round () function is used to keep the required decimal places for the execution time. For example, the time required for calculating the 1 million cycle is 0.116 seconds, for example:

To streamline the program, we can write this code into a class, introduce it when it is used, and instantiate this class before the program starts, and call another method to implement this function.

...

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.