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.

Tags: , ,     Posted in Development

Rate this article:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Further Reading

Leave a Reply