php time()和microtime()有时不一致 [英] php time() and microtime() do sometimes not concord

查看:213
本文介绍了php time()和microtime()有时不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用microtime()(使用PHP 5)记录一些数据时,相对于日志文件的时间戳,我遇到了一些似乎有些异相的值,所以我只是尝试比较time()和microtime()和一个简单的脚本(usleep只是为了限制数据输出):

While logging some data using microtime() (using PHP 5), I encountered some values that seemed slightly out of phase in respect to the timestamp of my log file, so I just tried to compare the output of time() and microtime() with a simple script (usleep is just here in order to limit the data output):

<?php
for($i = 0; $i < 500; $i++) {
    $microtime = microtime();
    $time = time();
    list($usec, $sec) = explode(" ", $microtime);
    if ((int)$sec > $time) {
        echo $time . ' : ' . $microtime . '<br>';
    }
    usleep(50000);
}
?>

现在,由于$ microtime在$ time之前声明,因此我希望它会更小,并且永远不会输出任何内容;但是,显然不是这种情况,并且$ time时不时地比microtime()返回的秒还小,如以下示例(截断的)输出:

Now, as $microtime is declared before $time, I expect it to be smaller, and nothing should ever be output; however, this obviously is not the case, and every now and then, $time is smaller than the seconds returned from microtime(), as in this example (truncated) output:

1344536674 : 0.15545100 1344536675
1344536675 : 0.15553900 1344536676
1344536676 : 0.15961000 1344536677
1344536677 : 0.16758900 1344536678

现在,这只是一个很小的差距;但是,我观察到一些序列(相差超过一秒)...那么,这怎么可能呢?

Now, this is just a small gap; however, I have observed some series where the difference is (quite) more than a second... so, how is this possible?

推荐答案

如果您查看

If you look at the implementations of time and microtime, you see they're radically different:

  • time just calls the C time function.
  • microtime has two implementations: If the C function gettimeofday is available (which it should be on a Linux system), it is called straight-forward. Otherwise they pull of some acrobatics to use rusage to calculate the time.

由于C time调用只能精确到一秒,因此它也可能有意使用低保真时间源.

Since the C time call is only precise up to a second, it may also intentionally use a low-fidelity time source.

此外,在现代x86_64系统上,这两个C函数都可以实现通过查看某些CPU寄存器没有系统调用.如果您使用的是多核系统,则这些寄存器在各个核之间可能并不完全匹配,这可能是一个原因.

Furthermore, on modern x86_64 systems, both C functions can be implemented without a system call by looking into certain CPU registers. If you're on a multi-core system, these registers may not exactly match across cores, and that could be a reason.

造成差异的另一个潜在原因是NTPd(计时守护程序)或其他用户空间进程正在更改时钟.通常,应通过 adjtime 避免此类影响.

Another potential reason for the discrepancies is that NTPd(a time-keeping daemon) or some other user-space process is changing the clock. Normally, these kinds of effects should be avoided by adjtime.

所有这些基本原理都是深奥的.要进一步调试问题,您应该:

All of these rationales are pretty esoteric. To further debug the problem, you should:

  • Determine OS and CPU architecture (hint: and tell us!)
  • Try to force the process on one core
  • Stop any programs that are (or may be) adjusting the system time.

这篇关于php time()和microtime()有时不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆