2

So I created a file microtime.php and tested it on my Windows Server 2008 R2 64x Enterprises running nginx dedicated server.

<?php 
for ( $i = 0; $i <= 5; $i++ ) {
 echo time() . '<br />';
 echo microtime() . '<br />';
 echo '<br />';
}
?>

and I get this:

1371343916
0.52320400 1371343916

1371343916
0.52320400 1371343916

1371343916
0.52320400 1371343916

1371343916
0.52320400 1371343916

1371343916
0.52320400 1371343916

1371343916
0.52320400 1371343916

Why the results are the same and how to fix that ?

Instead I should get something like this:

1371345446
0.61877300 1371345446

1371345446
0.61878200 1371345446

1371345446
0.61878500 1371345446

1371345446
0.61878700 1371345446

1371345446
0.61878900 1371345446

1371345446
0.61879100 1371345446

Kind Regards.

Michael Härtl
  • 8,428
  • 5
  • 35
  • 62

3 Answers3

1

The short answer: The repetition of the value in microtime has to do with the frequency with which microtime is being updated. You're requesting a value many times between each update. Related questions have been asked before. How precise is the internal clock of a modern PC? was useful for me to understand why this is so. Also, search for "HPET" and "precision timers".

Better accuracy: You can get it to be more accurate by exploding microtime into microseconds and seconds and multiplying each one by 10^8 (say) and then adding them together again. Converting that to a string and echoing will show you more numbers to the right of the "seconds decimal". Aside: I call it being correct to the octosecond, because interestingly, there is no English word for it.

Here's some code to prove the point:

    echo 'MICROTIME: '.microtime()." ";
    list($microseconds,$seconds) = explode(' ',microtime());
    $precisedatetimestamp = number_format((($seconds * 100000000)  + ($microseconds * 100000000)),"0","","");
    settype($precisedatetimestamp,'string');
    echo ' ..RETURNED : '.number_format($precisedatetimestamp,0,"","").'<br />';

Loop through that a few thousand times and see the result. Even with that level of accuracy, you are not going to prevent the repetition, it will just mean fewer duplicates. Processors are fast (and getting faster) and can perform a great many instructions per seconds. It's a matter of your required level of accuracy for your purpose. It can only be as correct as the update frequency of the internal clock.

Community
  • 1
  • 1
Qayyum
  • 41
  • 3
0

On Mac OSX Lion, When I run your code, I get :

1371583001
0.01606600 1371583001

1371583001
0.01609400 1371583001

1371583001
0.01610400 1371583001

1371583001
0.01611200 1371583001

1371583001
0.01612100 1371583001

1371583001
0.01612900 1371583001

So it looks like you need to run more iterations to get the results to change in Windows.

iewebguy
  • 316
  • 3
  • 16
0

For the record,7.2 + windows (but I also tested on other machines with the same result).

<?php
for ( $i = 0; $i <= 1000; $i++ ) {
    $k1=microtime();
    usleep(1);
    $k2=microtime();
    if($k1==$k2) echo "error k1 and k2<br>";
}
?>

It has not replication.

Apparently, usleep forces to reload the correct time from the system.

Edit: time_nanosleep(0,1); also works (it sleep a nanosecond).

magallanes
  • 6,583
  • 4
  • 54
  • 55