How to: Display page loading time + number of queries

Do you ever asked yourself how long it took for your page to load, or how many sql queries were executed? If yes, here is a simple code to insert on your template.

Nothing hard here. Simply paste the following code on your footer.php file:

<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?>  seconds.

The get_num_queries() function returns the number of executed query during your page loading.

Credits goes to WordPress Tuto for this awesome recipe!

Related Posts

Related Posts

No related posts.

34 Responses

Nov 16 2008 08:52

thanks a lot.

Nov 16 2008 12:28

Thanks.

Nov 16 2008 13:45

Thanks! :) I was curious in the past but was told that it’s impossible.

By the way what are good numbers? I am trying to keep blog light and fast, it shows “27 queries in 0.368 seconds”.

Nov 16 2008 14:13

@Rarst: It’s hard to say what are “good numbers” because it also depends of your server. But for what I know, the numbers you got seems pretty decent to me :)

Nov 16 2008 21:06

Great tip — not every theme includes this by default, but it’s definitely useful information when developing your site.

Users don’t really need to know — and especially not search engine bots, so you could use something like this to only output it for logged in users:

<?php if (is_user_logged_in()) { ?>
<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.
<?php } ?>

Regarding what numbers are good, it’s the # of SQL queries which is the big one, especially if you are on shared hosting. The fewer queries you have, the faster your site will be.

Enabling plugins 1 at a time will allow you to check how much each one is adding to the query count, perhaps allowing you to look for more efficient alternatives or “cut the fat” altogether.

Oh, WP-Super-Cache & WP Widgets Cache are two plugins you can use to significantly cut down on queries and processing time.

Nov 16 2008 21:17

@Rick Beckman: Thanks for your comments and all the useful info :)

Aug 02 2009 20:35

Thanks for the tip! :)

@Rick Beckman
Also thanks to you for enhancing the hack! ;)
I use WP-Super-Cache, WP Widgets Cache and DB-Cache for optimization. They help me a lot..

Dec 05 2009 15:38

Is there any method available that allows you to display the actual pageload time (as in the browser render time) for the end user..?

When your visitors watch your page load and it takes 3, or 5, or 7 seconds, they usually can’t understand why your trying to tell them it has loaded in (often) under 1 second. So it kinda looks misleading, because they associate the “load time” of the above database call with the actual page render.

Dec 05 2009 17:50

@Cracks

I think it might be possible to time actual page load client-side with JavaScript, but don’t know any ready-made solutions.

Anyway this is more of debug info, I see little reason to show it to visitor anyway.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required