2013-06-28 52 views
2

所以我衡量的執行時間是這樣的:PHP執行時間測量精度

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

PHP版本是贏Server 2008的64位機器上運行5.3.26RC1 86。 「精度」ini參數等於16. 但是,每次執行時間低於0.001時,它只返回零(顯然,腳本不能立即執行!)。 更奇怪的是,即使這兩個語句之間沒有代碼,我的測試機器(標準php.ini-development)也會返回非零值。什麼可能是錯誤的?

UPD:簡單的測試:

<?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; 
?> 

試機輸出:

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

生產機輸出:

Windows NT XXX 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586 
5.3.26RC1 
16 
0.0000000000 
+5

你要價太高(特別是在Windows)......你真的不應該指望低於0.001秒精度,尤其是在腳本語言。 – rid

+1

你能展示顯示時間的代碼嗎?當我使用代碼嘗試'microtime(true)'時,它顯示13位小數(默認PHP設置,在Windows上)。你確定你沒有格式化你的輸出嗎? – newfurniturey

+0

如何印刷價值? – sectus

回答

5

平臺,Windows中,不能這麼小的時候區別對待,與標準時間呼叫。

從PHP源代碼中的註釋:

「在Windows中,time_sleep_until具有毫秒精度,同時microtime中()是精確到秒的第十」

(在ext/standard/tests/misc/time_sleep_until_basic.phpt