Page Rendering Time

A lot of websites show something like “page rendered in 0.23 seconds” near the bottom. Not quite something necessary, but still a very cool feature, and useful if you ever have a long-loading page (such as the first page of most blogs, which displays the last ten posts — a heavy database call).

How can we code something like this? This is a feature we wanted to add to Launchpad; the answer was surprisingly easy.

To fetch the current (server) time, you can use Time.now, a Ruby method, to get the current time. The best thing about Time.now is that you can subtract two instances to get a time difference in fractional seconds.

So how can you use this? Easy! In your header (or at the top of your application-wide layout), add something like:

rendering_start = Time.now

Then, in the footer (or the bottom of your application-wide layout), add:


rendering_stop = Time.now
rendering_time = rendering_stop - rendering_start

<%= "Rendered in #{rendering_time} seconds" %>

And that’s it! The only caveat that if your rendering_start and rendering_stop wraps static HTML, the time to render that is included in your page rendering time. But, of course, you would expect that time to be negligible, compared to the time of the actual page rendering.

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development and tagged , , . Bookmark the permalink.

One Response to Page Rendering Time

  1. Phil Bogle says:

    Setting @rendering_start at the beginning of the action would include the time spent in the controller and better reflect the bulk of the actual time spent waiting by the user.