我已经在PHP中创建了一个基本应用程序,并希望打印在页面末尾加载页面所需的时间。我首先用microtime定义开始,然后将它与最后的当前microtime进行比较,问题是输出类似于" 0.000102"而且我在几毫秒内寻找它,我猜这将是102毫秒?
定义开始,
define("START", microtime(true));
然后打印结束时间。
printf("Page was rendered in %f milliseconds", (microtime(true) - START));
但它仍然输出那可怕的长串。
答案 0 :(得分:1)
毫秒为n*1000
:
<?php
$start = microtime(true);
usleep(1000000);
$end = microtime(true) - $start;
printf("Page was rendered in %f seconds", $end);
printf("Page was rendered in %f milliseconds", $end*1000);
printf("Page was rendered in %f microseconds", ($end*1000)*1000);
<强>结果:强>
Page was rendered in 1.000115 seconds
Page was rendered in 1000.115156 milliseconds
Page was rendered in 1000115.156174 microseconds
修改:如果您希望输出的值如0.10
等,则需要将%f
更改为%s
并使用round()
。
<?php
$start = microtime(true);
usleep(1000000);
$end = microtime(true) - $start;
printf("Page was rendered in %s seconds", round($end, 2));
printf("Page was rendered in %s milliseconds", round($end*1000, 2));
printf("Page was rendered in %s microseconds", round(($end*1000)*1000, 2));
<强>结果:强>
Page was rendered in 1 seconds
Page was rendered in 1000.12 milliseconds
Page was rendered in 1000121.12 microseconds