PHP Script Timer
Submitted by adam on Thu, 2008-01-31 19:07.
Every once in awhile I want to time the execution of certain areas of a script to weed out the slow parts. Every time I do it I end up making a new function like the one below. I'm sticking this here so I don't have to do it again next time. The code is real basic and can be extended easily to match your actual needs.
<?php
class timer {
function timer() {
$this->start = microtime(true);
}
function stop($show = true) {
$this->end = microtime(true);
$this->time = $time = $this->end - $this->start;
if ($show) echo "Execution time: {$this->time} seconds";
}
}
Example:
<?php $timer = new timer; // do heavy lifting here $timer->stop();
This code only works for PHP 5. If you're using PHP 4 you should really upgrade but in the meantime read Example #1 on the microtime manual page to get it working.
