Currently Browsing: Home » Faster Page Generation in WordPress

Faster Page Generation in WordPress

In the modern age of the web, speed is key. Gone are the days when you opened a web page, and went outside for twenty minutes before coming back to find that the web page is almost finished loading. Even if there are still people on dial-up connections, the modern world demands speed.

stopwatch

In the past, one of the best ways to improve speed was to reduce the size and amount of the components of the webpage. More specifics of this are outlined in an older LC article about decreasing page load time.

But when we’ve done all we can to reduce page loading time, we can still optimize further by locating and removing/optimizing bottlenecks in the code.

WordPress pages are all dynamically generated, meaning that they are recreated every time someone opens a page. Dynamically generated pages have many pros and a few cons. One of these cons is that it takes time to generate the page before sending it to the user, whereas static pages are immediately sent.

While many dynamic pages are coded in a way that allows them to be generated very quickly, sometimes blocks of code can take more time than intended or thought. This can lead to webpages taking a very long time to generate. Fortunately, there is a way to identify these blocks by adding small bits of code into WordPress.

WordPress has a function called timer_stop(). What this function does is that it returns the amount of time, in seconds, that has passed between when the server started generating the page to the time that the function was called.

For example, the following code:

<!-- <?php echo timer_stop(); ?> seconds passed. -->

would produce the number of seconds passed in an HTML comment.

timer_stop() can be called as many times as you want in WordPress’s code, which becomes useful for doing the following:

<!-- <?php echo timer_stop(); ?> seconds passed. -->
// Fast block of code
<!-- <?php echo timer_stop(); ?> seconds passed. -->
// Slow block of code
<!-- <?php echo timer_stop(); ?> seconds passed. -->

Let’s say this produced the following numbers: 0.01, 0.05, 0.32.
By matching up the numbers to the positions of the code, I could then determine that the slow block of code took .27 seconds to generate, while the fast block of code took only .04 seconds.

By using this method, you can identify bottlenecks in your WordPress code and remove or optimize them to make page generation faster.

Hope this helps you make your WordPress installation faster.

Tags:

This entry was posted on Thursday, January 21st, 2010 at 05:00:51. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Faster Page Generation in WordPress”

  1. Matthew Kirk says:

    This is an awesome little tip!

    Measuring how long it takes for certain elements to load in i.e. header, sidebar, footer. Then starting logging loading times for individual elements within.

    Thanks, I will abuse this shortly.

  2. Kenneth says:

    Thanks for the tip!

    Been testing this on my site now, and get a loading time around 0.3-0.5 sec.
    What loading time should I expect from my host? Seems that only the header uses 0.2 sec alone.

Leave a Reply

Want to be notified when someone replies? Subscribe to this post's comment RSS feed.
Any field marked with a * is required.