dongli8862 2013-06-28 06:26
浏览 48
已采纳

PHP执行时间测量精度

So I measure execution time like this:

$time = -microtime(true);
...
$time += microtime(true);

PHP version is 5.3.26RC1 x86 running on Win Server 2008 x64 machine. "Precision" ini parameter equals 16. However, each time the execution time is below 0.001, it just returns zero (obviously, the script can't be executed instantly!). What's even more strange, my test machine (standard php.ini-development) returns nonzero values even if there is no code between these two statements... What could be wrong?

Upd: Simple test:

<?php
ini_set('precision',16);
echo php_uname() . PHP_EOL;
echo phpversion() . PHP_EOL;
echo ini_get('precision').PHP_EOL;

$time = -microtime(true);
$time += microtime(true);
echo number_format($time, 10) .PHP_EOL;
?>

Test machine output:

Windows NT XXX 6.1 build 7601 (Windows 7 Ultimate Edition Service Pack 1) i586
5.4.12
16
0.0000028610

Production machine output:

Windows NT XXX 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586
5.3.26RC1
16
0.0000000000
  • 写回答

1条回答 默认 最新

  • dongya4089 2013-06-28 06:31
    关注

    Your platform, Windows, can not discriminate between such small times, with the standard time calls.

    A comment from the PHP source code:

    "on windows, time_sleep_until has millisecond accuracy while microtime() is accurate to 10th of a second."

    (In ext/standard/tests/misc/time_sleep_until_basic.phpt.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?