I have been asked with questions like “how does Wordpress get its code execute time” or “how to benchmark my php code” so many times already, so I decided to share a simple benchmarking class that I have written for debugging purposes. I have been using this to test functions before implementing them in my projects.

class Benchmark {
	protected $s, $e, $r;
	public function start(){
		$this->s = microtime(true);
	}
	public function exec_time() {
		$this->e = microtime(true);
		$this->r = number_format($this->e - $this->s, 4);
		return $this->r;
	}
}

Usage:

<?php
$bench = new Benchmark();
$bench->start();
//..
// code here..
// ..
echo $bench->exec_time();
?>

Please note that execution time will be in seconds.

Download the source code