put your head on my shoulder PHP Calculate code execution time and fix common errors online

WBOY
Release: 2016-07-29 08:45:08
Original
1117 people have browsed it

Copy the code 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 out after a little try There is a serious problem with this code. Although the time obtained by t2 is definitely greater than that of t1, it does not mean that its number of microseconds must be greater than the number of microseconds of t1. Therefore, if it is directly subtracted, the ms part may be obtained. is a negative number. Therefore, I changed it slightly and the code is as follows:

Copy the code The code is as follows:


$t1 = microtime(true);
// ... Execute the code...
$ t2 = microtime(true);
echo 'Time consuming'.round($t2-$t1,3).'Seconds';


Simply say. If microtime() takes a true parameter, it will return a Floating point type. In this way, what t1 and t2 get is two floating point numbers, and the difference between them is obtained after subtraction. Since the number of digits in the floating point is very long, or it is uncertain, use round() to get 3 decimal places. bit. In this way, our goal is achieved~

The above has introduced put your head on my shoulder PHP calculation code execution time-consuming code correction common online errors, including put your head on my shoulder content, I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template