PHP Computational code execution time-consuming Code correction online common error _php tips

Source: Internet
Author: User
Copy Code code as follows:

$t 1 = Explode (', microtime ());
// ... Execute code ...
$t 2 = Explode (', microtime ());
Echo ($t 2[1]-$t 1[1]). ' S '. ($t 2[0]-$t 1[0]). ' Ms ';

In fact, a little trial, you can find that the code has a serious problem. Although T2 time is certainly larger than T1, but does not mean that its microseconds must be more than the number of T1 microseconds. So if you subtract directly, the MS part is likely to get a negative number. So I changed it myself a little bit, and the code was as follows:
Copy Code code as follows:

$t 1 = microtime (true);
// ... Execute code ...
$t 2 = Microtime (true);
Echo ' time consuming '. Round ($t 2-$t 1,3). ' Seconds ';

Simply say. Microtime () returns a floating-point type if with a true argument. So t1 and T2 get two floating-point numbers and subtract the difference between them. Because the number of floating-point digits is very long or uncertain, use a round () to remove 3 digits from the decimal point. So our goal is reached

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.