04 Feb
by: Matt in Development, PHP
tags: execution time, microtime, php, profiling
Had to quickly get the execution time of certain segments of code so I whipped up this basic profiling class below.
Usage would go something like:
<?php $profiler = new cProfiler; $profiler->start(); // ... long block of code ... $et = $profiler->end(); echo 'longBlock et = '.$et.'<br>'; ?>
Class listing cProfiler.php:
<?php
class cProfiler
{
public $_i;
public $stamps;
public $ets;
function __construct()
{
$this->_i = 0;
$this->stamps = array();
$this->ets = array();
}
function __destruct() {}
function _stamp() { return microtime(); }
function start() {
$this->stamps[$this->_i] = $this->_stamp();
$this->_i++;
}
function getET($id = '') { return $this->ets[$id]; }
function end($id = '')
{
$timeend = $this->_stamp();
$et = false;
if($this->_i > 0) {
$timestart = $this->stamps[$this->_i - 1];
unset($this->stamps[$this->_i - 1]);
$this->_i--;
$et = number_format(((substr($timeend,0,9)) + (substr($timeend,-10)) - (substr($timestart,0,9)) - (substr($timestart,-10))),4);
$this->ets[$id] = $et;
}
return $et;
}
}
?>
Related posts:
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.