php time() and microtime() do sometimes not concord
Asked Answered
G

2

5

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

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?

Groth answered 9/8, 2012 at 18:35 Comment(2)
Could you tell me the outcome of some big upcoming matches? I'm want to place some bets ;)Honky
Can you tell us what OS you are using, what version of PHP this is happening in, and what version of libc/glibc PHP was compiled with? microtime works differently on Windows than it does on Linux, but both functions on Linux rely on C functions to get this info so likely this isn't really a PHP issue. I ran your code on PHP 5.4.5 and no time values were output (meaning time was always greater than or equal to microtime [as would be expected]).Simon
H
7

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

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

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.

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.
Highstrung answered 9/8, 2012 at 18:59 Comment(1)
Ok, thanks for the detailed hints. This definitely seems to be related to the OS, as it does occur on the two windows 7 machines I have at hand, but apparently not on the linux box of my hoster... As this might be a rather difficult problem to address, I guess I will just try to find some alternative to the usage of microtime...Groth
S
0

This could be due to the fact that microtime() uses floating point numbers, and therefore rounding errors may occur.

You can specify floating point numbers' precision in php.ini

Scorpius answered 6/3, 2013 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.