Unstable load times for JS and PHP

I have a PHP script that is loaded by JS via jQuery $.ajax . I measured the runtime of a PHP script using:

 $start = microtime(); // top most part of code // all other processes that includes AES decryption $end = microtime(); // bottom part of code file_put_contents('LOG.TXT','TIME IT TOOK: '.($end-$start)."\n",FILE_APPEND); 

It is measured somewhere less than 1 second. There are no scripts to add / add PHP.

In JS $.ajax I measured the runtime:

 success: function(response) { console.log(date('g:i:s a') + ' time received\n'); // all other processes including AES decryption console.log(date('g:i:s a') + ' time processed\n'); } 

The time will be the same for the received time and the processed time.

However, when I check the Chrome Developer Tools , it claims that the PHP script has been loaded for about 8 seconds .

What could be wrong with the way I measured these things? I'm sure PHP loads quickly, but why does Chrome report that it took more than 8 seconds?

I use localhost and my web server is fast and this is the only time I have encountered this problem. All other AJAX calls are quick.

+6
source share
1 answer

In the PHP section, make sure you use microtime(true) to work with floating point numbers instead of strings . Using row subtraction can lead to incorrect results.


Example: http://ideone.com/FWkjF2

 <?php // Wrong $start = microtime(); sleep(3); $stop = microtime(); echo ($stop - $start) . PHP_EOL; // Prints 8.000000000008E-5 // Correct $start = microtime(true); sleep(3); $stop = microtime(true); echo ($stop - $start) . PHP_EOL; // Prints 3.0000791549683 ?> 
+2
source

All Articles