Skip to content

Instantly share code, notes, and snippets.

@scarstens
Created September 18, 2012 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scarstens/3744576 to your computer and use it in GitHub Desktop.
Save scarstens/3744576 to your computer and use it in GitHub Desktop.
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