我使用了microtime()来检查代码执行时间。但这似乎很奇怪,时间跟踪是不正确的。
因此,在我的test.php中,我有如下代码:
$debug = true;
$start = microtime(true);
$newline = "<br/>";
...
if ($debug) {
$time_elapsed_secs = microtime(true) - $start;
$start = microtime(true);
echo 'Step 1 Done: ' . $time_elapsed_secs . $newline; }
...
if ($debug) {
$time_elapsed_secs = microtime(true) - $start;
$start = microtime(true);
echo 'Step 2 Done: ' . $time_elapsed_secs . $newline; }然后,当我在浏览器上打开URL时,它会在不到1秒内响应,但是它显示了一些奇怪的值,比如步骤1完成: 0.0026565步骤2完成: 9.8646454
怎么会发生这种事?我是不是做错了什么事?
发布于 2015-10-13 17:37:21
我猜你在描述中遗漏了一个小细节。我觉得你实际看到的结果更像是.
Step 1 Done: 0.0026565
...
Step 2 Done: 9.8646454E-5当浮点数低于0.0001时,PHP会将它们放入科学符号中。要使输出中的内容一致,请尝试将代码更改为下面的代码,以小数点表示法显示微时间。
$debug = true;
$start = microtime(true);
$newline = "<br/>";
usleep(30);
if ($debug) {
$time_elapsed_secs = microtime(true) - $start;
$start = microtime(true);
echo 'Step 1 Done: ' . sprintf( '%f', $time_elapsed_secs ) . $newline; }
usleep(1);
if ($debug) {
$time_elapsed_secs = microtime(true) - $start;
$start = microtime(true);
echo 'Step 2 Done: ' . sprintf( '%f', $time_elapsed_secs ) . $newline; }注意:添加us休眠()调用来显示正在发生的事情。
发布于 2015-10-13 15:56:39
这取决于这两个步骤之间的代码。注释掉这两个步骤之间的代码(但您是对的,如果页面返回不到1秒,这是很奇怪的。))
// Sleep for a while
usleep(100);检查微时间是否测量正确的时间增量。
https://stackoverflow.com/questions/33107182
复制相似问题