PHP Benchmarking Script - Timer
Time the execution of your script using this simple script.
I made a small and simple benchmarking script to time a rather processor intensive script I developed lately. I wrapped it inside a class in case I have the need to use it again.
class Timer {
var $start_time = 0;
var $end_time = 0;
function Timer($start=0) {
if($start) $this->start();
}
function start() {
$timeparts = explode(' ',microtime());
$this->start_time = $timeparts[1].substr($timeparts[0],1);
}
function stop($show=1,$decimal_place=8) {
$timeparts = explode(" ",microtime());
$this->end_time = $timeparts[1].substr($timeparts[0],1);
if($show) $this->display($decimal_place);
}
function display($decimal_place=8) {
echo number_format(bcsub($this->end_time,$this->start_time,6),$decimal_place);
echo "s";
}
}
You can call the script like this...
$T = new Timer();
$T->start();
// ... do whatever that must be timed ...
$T->stop();
Execution time for this page : 0.00026417s

Comments
static $start=0;
static $end=0;
public static function start() {
$timeparts = explode(' ',microtime());
self::$start = $timeparts[1].substr($timeparts[0],1);
self::$end=0;
}
public static function stop($show=1,$decimal_place=5) {
$timeparts = explode(" ",microtime());
self::$end = $timeparts[1].substr($timeparts[0],1);
if($show) self::display($decimal_place);
}
private static function display($decimal_place=5) {
$time = number_format(self::$end - self::$start,$decimal_place);
$mem=memory_get_peak_usage(true);
$_max=ini_get("memory_limit");
$max=explode("M",$_max);
$max=$max[0]*1048576;
$usage=round(($mem*100)/$max,1);
if ($mem>=1000000) { $mem=round($mem/1048576,2)." Mb"; }
elseif ($mem>=1000) { $mem=round($mem/1024,2)." Kb"; }
else { $mem="$mem b"; }
echo "
<div style='
position: absolute;
position: fixed;
right: 5px;
width: auto;
bottom: 5px;
font: Arial, Helvetica, sans-serif;
font-size: 12px;
color: Black;
padding: 2px;
border: solid #AAA 1px;
text-align: left;
background-color: #DFDFDF;
filter: alpha(opacity=65);
-moz-opacity: .65;
overflow:hidden;
'>
<div>Processing Time: {$time}s</div>
<div>Memory Usage: $mem ($usage% of $_max)</div>
</ul>
</div>
";
}
}
require_once("benchmark.php");
benchmark::start();
{ ... do something ... }
benchmark::stop();
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.