samScripts.com
[Home] [My Scripts] [Contact] [Business Card Designer] [Donate] [$8.95 Domain Names]

Home > Scripts > timerclass

timerclass

... feel free to use this script for whatever you like.

timerclass

This is a simple, easy to use class for timing how long all or parts of your scripts take.

Description:

Usage:

To get the total time of a script, put this at the top:


<?php
include("./timer.php");

$timer = new timer(); // starts timing


And this somewhere near the end:

echo 'Script took '.$timer->gettime('total', 4).' seconds to run.';


The name 'total' refers to the main timer. The four is the number of decimal points to display.

You can also have other named timers for bits of your script.

For example, if you wanted the time a database query takes to execute, and the time it takes to display the results, together with the total for both:


<?php
// beginning of script would be here

$sql = 'SELECT somestuff FROM sometable';

$timer->start('queryandresults'); // time for everything

$timer->start('query'); // time for query

$results = mysql_query($sql);

$timer->stop('query'); // query done so stop timing it

$timer->start('display'); // start timing display

while( list($somestuff) = mysql_fetch_row($results)){
echo
'This is '.$somestuff.'<br>';
}

$timer->stop('display'); // stop displaying results

$timer->stop('queryandresults'); // stop timing everything

// display the results

echo 'The total time for query and display was: '.$timer->gettime('queryandresults');
echo
'<br>The time taken in the query was: '.$timer->gettime('query');
echo
'<br>The time taken displaying results was: '.$timer->gettime('display');

// alternatively, display all times with one command:

$timer->showtimes(4, "<br>The time taken for %name was %time seconds.");

// which also shows the total script time, as well as the times for any other named timers.


Files

timerclass.php (1788 bytes) View Download
Viewed 1008 times. Downloaded 1461 times.

[Back] [Top] [Forward]

Powered by php, mySql.

Request processed in 0.01 seconds on Wednesday 26th September 2012 23:43:08.

©2002 Sam Yapp

What do you think of my resume wizard site? All nicely written in php :)