PHP calculates code execution time to fix common bugs on the web
- 2020-05-05 11:00:52
- OfStack
$t1 = explode(' ', microtime());
// ... Execute the code ...
$t2 = explode(' ', microtime());
echo ($t2[1]-$t1[1]).'s '.($t2[0]-$t1[0]).'ms';
In fact, there is a serious problem with this code after a little trial. Although the time of t2 is definitely greater than t1, it does not mean that its microseconds are necessarily greater than the microseconds of t1
$t1 = microtime(true);
// ... Execute the code ...
$t2 = microtime(true);
echo ' Time consuming '.round($t2-$t1,3).' seconds ';
If microtime() takes an true parameter, it will return a floating point type. Then t1 and t2 will get two floating point Numbers, and the difference between them will be obtained