我有一段糟糕的時間讓自己相信我在這裏所做的是一個好主意。具體的部分,我覺得反感的是:在PHP中使用microtime和浮點數不精確的數字
return ((float)($now+$sec).'.'.$mic);
爲了保持浮點精度,我被迫要麼依傍BC或GMP庫(均未始終可用)。在這種情況下,我使用串連接干擾了數字。
<?php
// return the current time, with microseconds
function tick() {
list($sec, $mic, $now) = sscanf(microtime(), "%d.%d %d");
return ((float)($now+$sec).'.'.$mic);
}
// compare the two given times and return the difference
function elapsed($start, $end) {
$diff = $end-$start;
// the difference was negligible
if($diff < 0.0001)
return 0.0;
return $diff;
}
// get our start time
$start = tick();
// sleep for 2 seconds (should be ever slightly more than '2' when measured)
sleep(2);
// get our end time
$end = tick();
$elapsed = elapsed($start, $end);
// should produce output similar to: float(2.00113797188)
var_dump($elapsed);
?>
如果我嘗試添加兩個數字像(代表時間戳)和0.0987654321(代表微秒),使用加法運算符(+)我總是用123456789.099結束。即使將整數轉換爲浮點數,結果也是一樣的。
是否有解決這個問題的方法是1)不是破解和2)不涉及字符串連接?爲了獲得精確的微秒分辨率時間戳,我不應該回頭看這種亂碼。
編輯:正如S. Gehrig所解釋的,PHP中的浮點數有時可能會有點棘手顯示。 PHP配置中指示的「精度」與顯示有關。實際值並不像我想象的那樣圓整。一個簡單得多的解決方案上面的代碼看起來像這樣:
// return the current time, with microseconds
function tick() {
return microtime(true);
}
// compare the two given times and return the difference
function elapsed($start, $end) {
return $end-$start;
}
// get our start time
$start = tick();
// sleep for 2 seconds (should be ever slightly more than '2' when measured)
sleep(2);
// get our end time
$end = tick();
$elapsed = elapsed($start, $end);
// should produce output similar to: float(2.00113797188)
var_dump($elapsed);
如果你要檢查$開始或$從另一箇中減去一個之前結束,它可能會出現,他們舍入到百分之一位置。不是這種情況。在顯示有限的情況下,似乎任意的算術精度被保留。
我會的,但精度是舍入到2個地方,似乎沒有辦法改變這一點。默認情況下,PHP的精度是12位......但這個函數並不尊重它。檢查它: php -r'echo microtime(true);' 我不知道是什麼原因導致它圓形的。 – spligak 2009-06-23 08:03:38
float的12位小數的精度INCLUDES逗號前的小數 - 所以12位精度就是您所看到的。 – 2009-06-23 08:19:01