Page Load Speed: microtime() is much faster than Google Developer Tools


I’m trying to speed up the load time on my site. Google Developer Tools says my main HTML/TEXT page takes 2.18 seconds to load (Waiting TTFB). I assume this has something to do with inefficient PHP/MySQL scripts.

However, I use the following code to see how long the script loads:

$start_time = microtime(true); // very first line
echo "This page was generated in ".(microtime(true) - $start_time)." seconds."; // very last line

Apparently this results in a page loading time of 0.38 seconds.

Although this is wrong because the page really does load up in 2-3 seconds as Google Developer Tools indicated.

Can anyone explain this behavior? Any tips on speeding this up?

Kind regards

My guess is that is not wrong and the script is only taking a fraction of a second for the output to be generated.

Loading time on the other hand depends on more than what happens server-side, eg. image weight, number of HTTP requests to get referenced files, etc.

This is just one specific file getting loaded, even before download. Google Developer Tools shows the time it takes to load each image, which is separate than the main file. I’m not sure if you are familiar with this tool or not, but it’s only on Google Chrome.

The time spent inside PHP is only one small fraction of the number reported by developer tools. The “time to first byte” also contains things like the time spent going over the network (both to and from the web server), any delays in the web server itself (both to and from PHP).

Without more details about your particular set up, it’s going to be difficult to give specifics. If the server is remotely hosted that will contribute more to the TTFB than if it’s on the same machine as you’re accessing the file from. The web server configuration, and to some extent PHP configuration, can play their own parts in delaying getting information from PHP to the client browser.

I would start by looking at the different stages of the request. 1. the network, 2. the web server, 3. PHP.

So you send a request (0).

  1. DNS resolves the domain. (This may take several attempts)
  2. A GET/POST (/whateverotherHTMLverbsyouuse) request is sent to the server.
  3. The server’s web handler receives the request, evaluates the request, starts the PHP engine to process the file’s PHP content, and waits.
  4. PHP runs across your script for fatal errors, includes, etc.
  5. PHP runs the lines of your script. Your timer runs now.
  6. PHP returns the resultant HTML.
  7. The web handler forms the response based on PHP’s handling instructions (302, 200, 500, etc), and sends the HTML bytewise towards your browser, across the internet.
  8. Browser receives data (TTFB).
  9. Browser sends additional requests for each image, javascript/css include, etc. (Rinse, repeat steps 0-8 as necessary).
  10. Page presented to user.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.