PHP code execution time-consuming code correction common online errors _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP code execution time-consuming code fixes common online errors. Copy the code as follows: $ t1explode (, microtime ());... run the code... $ t2explode (, microtime (); echo ($ t2 [1]-$ t1 [1]). s. ($ t2 [0]-$ t1 [0]). ms; in fact, try it a little The code is as follows:


$ T1 = explode ('', microtime ());
//... Execute the code...
$ T2 = explode ('', microtime ());
Echo ($ t2 [1]-$ t1 [1]).'s '. ($ t2 [0]-$ t1 [0]). 'Ms ';


In fact, you can find this code has a serious problem. although the time obtained by t2 is certainly greater than that obtained by t1, it does not mean that the number of microseconds must be greater than that of t1. so directly subtract, the MS part may get a negative number. therefore, I made a slight change and the code is as follows:

The code is as follows:


$ T1 = microtime (true );
//... Execute the code...
$ T2 = microtime (true );
Echo 'elapsed Time'. round ($ t2-$ t1, 3). 'second ';


To put it simply. microtime () if it contains a true parameter, a floating point type is returned. in this way, t1 and t2 get two floating point numbers, and the difference is obtained after subtraction. because the number of floating point digits is very long or uncertain, we can use a round () to retrieve the three digits after the decimal point. in this way, our goal is achieved ~

The http://www.bkjia.com/PHPjc/323343.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323343.htmlTechArticle code is as follows: $ t1 = explode ('', microtime ());//... run the code... $ t2 = explode ('', microtime (); echo ($ t2 [1]-$ t1 [1]).'s '. ($ t2 [0]-$ t1 [0]). 'Ms'; in fact, try it a little...

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.