PHP Calculation code Execution time-consuming code correction online Universal Error _php tutorial

Source: Internet
Author: User
Copy CodeThe code is 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 bit of a try, you can find this code has a serious problem. Although T2 get more time than T1, but does not mean that its microsecond number is certainly greater than the number of microseconds T1. So if you subtract directly, the MS section is likely to get negative numbers. So, I changed a little bit myself, the code is as follows:
Copy CodeThe code is as follows:
$t 1 = microtime (true);
// ... Execute code ...
$t 2 = Microtime (true);
Echo ' time-consuming '. Round ($t 2-$t 1,3). ' Seconds ';

Simply say it. Microtime () If a true argument is taken, the return will be a floating-point type. So t1 and T2 get is two floating point number, subtract and get the difference between. Because the number of bits in the floating point is very long, or is not sure, then use a round () to remove the decimal point 3 bits. So our purpose is to reach the ~

http://www.bkjia.com/PHPjc/323343.html www.bkjia.com true http://www.bkjia.com/PHPjc/323343.html techarticle Copy the 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 try ...

  • 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.