Displaying page generation time on Drupal pages

Published Tue, 2006/12/26 - 21:02

One often needs to display the page generation time for every page.

The excellent devel module provides this feature, and other important ones as well, like displaying queries and the time it takes each one to execute, and much more.

However, there are sometimes side effects to enabling the devel module on 4.6 and 4.7, that may manifest itself in certain situations. For example, showing errors on the screen directly.

If you want the timer functionality without the full devel, here is a tiny module that displays the timer at the bottom of every page.

To configure it, you have to add the following to the bottom of your settings.php file.

   $conf = array(
'dev_timer' => 1,
);

Then copy the following code to a file named zztimer.module in your modules directory. Enable it like any other module.

<?php

define('TIMER_PRINT', 'dev_timer');

function zztimer_exit() {
global $timer;

if (variable_get(TIMER_PRINT, FALSE)) {
list($usec, $sec) = explode(' ', microtime());
$stop = (float)$usec + (float)$sec;
$diff = round(($stop - $timer) * 1000, 2);
$diff = number_format($diff, 2);
print t('Page generated in %time ms.', array('%time' => $diff));
}
}

Enjoy!

Output

Sorry, but what how do I convert output into seconds?

Divide by 1000

1 second = 1000 milliseconds.
--
2bits -- Drupal consulting

Typo?

Isn't there a '?>' missing at the end of that?

My drupal site went dead when I inserted this code -- even with the missing ?>

No

PHP files do not have to end with ?>, and that includes .module files.

Did you create that as a .module or pasted it in a node or what?
--
2bits -- Drupal consulting