Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP Benchmark Timer
$timer = new timer(1); // constructor starts the timer, so no need to do it ourselves
/*
... mysql query ...
*/
$query_time = $timer->get();
/*
... page processing ...
*/
$processing_time = $timer->get();
class timer {
var $start;
var $pause_time;
/* start the timer */
function timer($start = 0) {
if($start) { $this->start(); }
}
/* start the timer */
function start() {
$this->start = $this->get_time();
$this->pause_time = 0;
}
/* pause the timer */
function pause() {
$this->pause_time = $this->get_time();
}
/* unpause the timer */
function unpause() {
$this->start += ($this->get_time() - $this->pause_time);
$this->pause_time = 0;
}
/* get the current timer value */
function get($decimals = 8) {
return round(($this->get_time() - $this->start),$decimals);
}
/* format the time in seconds */
function get_time() {
list($usec,$sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment