Page generated in seconds x.xxxx

You’ve probably noticed at some sites a little message, mainly in the page footer, type ‘ Page generated in 0th seconds …. ‘or something similar. Here you can learn how to easily make using PHP microtime() function
The code consists of two parts:
At the top of your PHP pages, primarily put the following code:

// Put on top of the document
$load_time = microtime();
$load_time = explode(' ',$load_time);
$load_time = $load_time[1] + $load_time[0];
$page_start = $load_time;

and near the end of the page (before ), insert the following code:

//Put an end of document

$places = 4; //how many decimal places you want

$load_time = microtime();
$load_time = explode(' ',$load_time);
$load_time = $load_time[1] + $load_time[0];
$page_end = $load_time;
$final_time = ($page_end - $page_start);
$page_load_time = number_format($final_time, $places, '.', '');

echo("Page generated in " . $page_load_time . " seconds");

Microtime() function returns a Unix timestamp (timestamp) for the moment

In WordPress:
First, add this into your file wp-config.php file so that data about each MySQL query is stored, namely the time taken.

define( 'SAVEQUERIES', true );

Then in your footer.php, use this code to display it:

global $wpdb; 
// Get the total page generation time
$totaltime = timer_stop( false, 22 );
// Calculate the time spent on MySQL queries by adding up the time spent on each query
$mysqltime = 0;
foreach ( $wpdb->queries as $query )
    $mysqltime = $mysqltime + $query[1];
// The time spent on PHP is the remainder
$phptime = $totaltime - $mysqltime;
// Create the percentages
$mysqlper = number_format_i18n( $mysqltime / $totaltime * 100, 2 );
$phpper   = number_format_i18n( $phptime / $totaltime * 100, 2 );
// Output the stats
echo 'Page generated in ' . number_format_i18n( $totaltime, 5 ) . " seconds ( {$phpper}% PHP, {$mysqlper}% MySQL )";

Calculation is simple. Manipulate the string in a way to write the start and end time for the page and then simply subtract one from another and so we get the load time of pages.

Leave a Reply