I made a generic timer class that can be used to time anything; queries, connection times, page generation times, etc.
It goes like this:
<?php
class Timer {
var $start;
var $finished;
var $elapsed;
function Timer() {
if($this->start) {
return;
}
list($u,$s) = explode(' ', microtime());
$this->start = (float)$u + (float)$s;
return $this->start;
}
function Stop() {
list($u,$s) = explode(' ', microtime());
$this->finished = (float)$u + (float)$s;
$this->elapsed = $this->finished - $this->start;
return $this->finished;
}
function Elapsed() {
if($this->start) {
if(!$this->finished) {
$this->Stop();
}
$this->elapsed = $this->finished - $this->start;
return $this->elapsed;
}
return;
}
}
?>
You would implement it like this:
$q = "SELECT * FROM sometable";
$t = new Timer();
$r = mysql_query($q);
if(!$r || !mysql_num_rows($r)) {
// query returned no results or failed, so we stop the timer:
$t->Stop();
} else {
// Stop the timer here if you're just interested in how fast MySQL got a result set.
// echo($t->Elapsed());
while($data = mysql_fetch_array($r)) {
// do stuff with the results
}
}
// Stop the timer and get the elapsed time:
echo $t->Elapsed();
Works for me... have fun...