The script that shows the date and time in milliseconds
then falls asleep on usleep(20000);
And again displays the date

 $microt = explode(" ",microtime()); echo $time = date("YmdHis",$microt[1]).substr((string)$microt[0],1,4); echo "<br>"; usleep(20000); $microt = explode(" ",microtime()); echo $time = date("YmdHis",$microt[1]).substr((string)$microt[0],1,4); echo "<br>"; 

// run the script in turn 3 times // display the script

 //попытка 1 2016-10-28-20-29-57.909 2016-10-28-20-29-57.929 //попытка 2 2016-10-28-20-30-27.222 2016-10-28-20-30-27.243 //попытка 3 2016-10-28-20-30-51.911 2016-10-28-20-30-51.931 

Attempt 1 and 3 worked fine, but attempt 2 instead of a difference of 0.20 brought the difference to 0.21, as you can see the script is no longer busy, what could be the problem?

  • If it's not a secret, why do you need such accuracy? - E_p
  • the result will be assigned to the file name because // file 1 example 2016-10-28-20-29-57.900 // file 2 example 2016-10-28-20-29-57.920 // file 3 example 2016-10- 28-20-30-51.911 // file 4 example 2016-10-28-20-30-51.931 but if it is not accurate, it will not look nice in the list, but has already solved this question for another - hovdev
  • Without all the jokes, it will be very interesting for me to look at your project when you finish it. When you finish the project skinte link. - E_p
  • no problem, this is its unpretentious cms system with the possibility of further development - hovdev

1 answer 1

The problem is not new, and very relevant for realtime computing / systems.

https://en.wikipedia.org/wiki/Real-time_computing

Many languages ​​do not provide sufficient accuracy in time (OS layer, rounding, type conversion, processor frequency is not stable and constant floats a little ...)

Also, in your code to display the date, you do quite a "time-consuming" conversion.

expload , date , substr - not the fastest functions add rounding to them ...

If you rewrite your code:

 <?php $t1 = microtime(true); usleep(20000); $t2 = microtime(true); echo $t2 - $t1; 

The result will be closer to what you want.

I started the test, the error turned out to be 10 times less than yours.

 0.02014684677124 0.020076990127563 0.020087957382202 0.020082950592041 0.020128011703491 0.020082950592041 0.02009105682373 0.02008581161499 0.020123958587646 0.020067930221558 
  • clearly, without error, apparently nothing at all - hovdev