HMV.co.in

September 23, 2008

Page load time

Filed under: php — Tags: , , , — Harsha M V @ 10:50 pm

Description

Outputs the time in seconds that it takes for a PHP page to load.

The code


<?php

// Insert this block of code at the very top of your page:

$time microtime();

$time explode(" "$time);

$time $time[1] + $time[0];

$start $time;

// Place this part at the very end of your page

$time microtime();

$time explode(" "$time);

$time $time[1] + $time[0];

$finish $time;

$totaltime = ($finish $start);

printf ("This page took %f seconds to load."$totaltime);

?> 

September 22, 2008

Script Timer

Filed under: php — Tags: , — Harsha M V @ 6:48 pm

This timer is the simplest and easiest to implement. It will determine the time taken for a php script to execute correct to 0.000000000000001 seconds.

<?php
$mtime
= microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
?>

<!-- put other code and html in here -->

<!-- put this code at the bottom of the page -->
<?php
$mtime
= microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo
'This page was created in ' .$totaltime. ' seconds.';
?>

PHP’s microtime() function returns the current Unix timestamp with microseconds. And the explode( string separator, string string [, int limit] ) function returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string separator. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

Blog at WordPress.com.