Tech@TheAtomicMoose.ca: PHP Timer Script

Subjects

PHP Timer Script

Timer Script

This is the only PHP script that I created and use nearly everyday but NEVER published! My stopwatch / timer script... I use it to test, benchmark most of the other scripts I write.

Sometimes, webmasters use something similar to show you Page Generation times at the bottom of a web page; not unlike what I have done myself at the bottom of this page: e.g.

code:
Vilitas.com created this page in 0.105253 seconds.

So, not only is this Timer / Stopwatch code good for figuring out how efficiently your code run, it also helps to add to your 'content'! :)

PHP Page Generation / Timer script

Copy the entire code below and save it as stopwatch.php if you want to use it as a benchmarking tool while you test your different PHP scripts.

php:

<?php

//  Filename: STOPWATCH.PHP
//  =======================

//  Start TIMER
//  -----------
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];

//  -----------

/* ------------------------------------- */
//  Add your PHP script and/or content here
/* ------------------------------------- */

//  End TIMER
//  ---------
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
echo '<p style="margin:auto; text-align:center">';

printf( "Script timer: <b>%f</b> seconds.", ($etimer-$stimer) );
echo '</p>';

//  ---------

?>


If you want to use it as a Page Generation tool, you can just add your content where it says

code:
/* ------------------------------------- */
//  Add your PHP script and/or content here
/* ------------------------------------- */

and rename the file... but that's not very smart, is it?

Including Page Generation times on all your web pages

The popular way to do it actually is to add 2 parts of this script into 2 different PHP include files:

code:
header.php
footer.php

and then just include the 2 files (header / footer) on every web page your web site outputs!

Sample header / footer.php

header.php

php:

<?php
//  Start TIMER
//  -----------

$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];

//  -----------
//  REST OF YOUR HEADER CODE...
?>


footer.php

php:

<?php
//  SOME CLOSING CONTENT IN YOUR FOOTER...
//  End TIMER
//  ---------
$etimer = explode( ' ', microtime() );

$etimer = $etimer[1] + $etimer[0];
echo '<p style="margin:auto; text-align:center">';
printf( "Script timer: <b>%f</b> seconds.", ($etimer-$stimer) );
echo '</p>';

//  ---------
?>


original url - http://www.desilva.biz/php/timer.html
Back