PHP Computing program run time Simple example sharing

Source: Internet
Author: User
Tags explode

Source: http://www.jb51.net/article/49974.htm

First of all, we analyze the principle, to get the program running time, then you can define a variable at the start of the program to write down the current time, and then after we run the program to record the current time, the difference is that the program to run the time spent.

Here's a microtime () This function, Microtime () not much, but not the function is not known, it is to return the current Unix timestamp and the number of microseconds. For example: Echo Microtime (); will return: 0.08845800 1376983061. So you can use the explode function to divide it into an array of spaces, then the $starttime[0]=0.08845800 (microseconds), $starttime [1]=1376983061 (the current number of seconds, equivalent to time () results).

Sample code:

Copy code code as follows:
<?php
Program Run time
$starttime = Explode (', microtime ());
Echo Microtime ();

/*········ The following is the code area */
For ($i =0 $i <1000000; $i + +) {
$i;
}
/*········ Above is code area */

Program Run time
$endtime = Explode (', microtime ());
$thistime = $endtime [0]+ $endtime [1]-($starttime [0]+ $starttime [1]);
$thistime = Round ($thistime, 3);
echo "This page is time-consuming to perform:". $thistime. "Seconds. ". Time ();
?>

The last two times subtract, and then use the round () function to the time of execution to retain their own required decimal bit is OK. For example, this is the time required to compute the loop 1 million times: 0.116 seconds, as shown in the following figure:

For the sake of the neatness of the program, we can write this code as a class, introduce it in, and then instantiate the class before the program starts, and then call a method to accomplish 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.