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.
|