Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Calculate webpage load time in PHP

Featured Replies

Insert at the beginning of page

PHP Code:

 

$m_time = explode(" ",microtime());

$m_time = $m_time[0] + $m_time[1];

$loadstart = $m_time;

 

 

insert at the end of page

PHP Code:

 

$m_time = explode(" ",microtime());

$m_time = $m_time[0] + $m_time[1];

$loadend = $m_time;

$loadtotal = ($loadend - $loadstart);

echo "<small><em>Generated page in ". round($loadtotal,3) ." seconds</em></small>";

I see you're not asking a question but posting some code others might find helpful - so thanks B)

 

One suggestion I'd make is to only do the explodes later - the reason being that if you are interesting in microsecond granularity, there's always a chance the explode() themselves will take extra processing time and upset the results. Therefore I'd make these ever-so-slight changes (and wrap them in functions I can include and call in every page):

 

<?php
/* loadtimes.php */
function loadtime() {
 return microtime(); // this doesn't work on all platforms (see man) so alternatives may be used there
}

function loadend($loadstart) {
 $loadend = loadtime();
 /* Now do all the processing here, after having taken the timings */
 $m_time = explode(" ",loadstart);
 $loadstart = $m_time[0] + $m_time[1];
 $m_time = explode(" ",loadend);
 $loadend = $m_time[0] + $m_time[1];
 $loadtotal = $loadend - $loadstart;
 echo "<p class=\"loadtime\">Generated page in ". round($loadtotal,3) ." seconds</p>";
}
?>

Then in each PHP page:

 

<?php
include "loadtimes.php";
$loadstart = loadtime();

// rest of page

loadend($loadstart);
?>

Really just a minor modification to avoid effects which are probably negligible in comparison to the rest of your page anyway, and make the code reusable. :rolleyes:

  • Author
I see you're not asking a question but posting some code others might find helpful - so thanks B)

 

One suggestion I'd make is to only do the explodes later - the reason being that if you are interesting in microsecond granularity, there's always a chance the explode() themselves will take extra processing time and upset the results. Therefore I'd make these ever-so-slight changes (and wrap them in functions I can include and call in every page):

 

<?php
/* loadtimes.php */
function loadtime() {
 return microtime(); // this doesn't work on all platforms (see man) so alternatives may be used there
}

function loadend($loadstart) {
 $loadend = loadtime();
 /* Now do all the processing here, after having taken the timings */
 $m_time = explode(" ",loadstart);
 $loadstart = $m_time[0] + $m_time[1];
 $m_time = explode(" ",loadend);
 $loadend = $m_time[0] + $m_time[1];
 $loadtotal = $loadend - $loadstart;
 echo "<p class=\"loadtime\">Generated page in ". round($loadtotal,3) ." seconds</p>";
}
?>

Then in each PHP page:

 

<?php
include "loadtimes.php";
$loadstart = loadtime();

// rest of page

loadend($loadstart);
?>

Really just a minor modification to avoid effects which are probably negligible in comparison to the rest of your page anyway, and make the code reusable. :rolleyes:

 

Thanks Connetu_C... :)

Every one else was doing it so fidgured I'd post my timer function also :)

 

///------------------------------------------
/// function to calculate parse time
///------------------------------------------

function funcParseTimer($vMarker='') { 
	$aTmp = explode (" ", microtime ()); 
	$vTime = $aTmp[1] + $aTmp[0];
	if (!defined('RICE_PARSE_TIME')) { define('RICE_PARSE_TIME',$vTime); srand((float) $aTmp[1] + ((float) $aTmp[0] * 100000)); }
	else { echo '<!-- '.$vMarker,': ',round(($vTime - RICE_PARSE_TIME),4)," seconds -->\r\n"; }
}

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.