Performansa çok dikkat eden biri olarak proje yapım aşamasında, her sayfa sonunda sayfa açılış hızı ve kaynak tüketimini yazdırıyorum. CodeIgniter’da bu bilgileri view dosyasında yazdırmak gayet kolay, fakat framework kullanmadığım zamanlarda aşağıdaki benchmark sınıfı ile istediğim kod bloğunun ne kadar süre aldığını elde edebiliyorum.
class benchmark { private $points = array(); function __construct() { $this->points['start'] = $this->microtime_float(); } private function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } public function point($p) { $this->points[$p] = $this->microtime_float(); } public function result($p1, $p2) { if ( ! isset($this->points[$p1]) OR ! isset($this->points[$p2]) ) return FALSE; return round($this->points[$p2] - $this->points[$p1], 4); } }
Kullanımı da şu şekilde;
require('class.benchmark.php'); $bench = new benchmark; $bench->point('buradan'); // burası kod bloğunuz.. $bench->point('buraya'); echo '<hr /> Sayfa çalışma süresi: <strong>' , $bench->result('buradan', 'buraya') , '</strong> saniye';




