PHP Classes

File: simpleprofiler.php

Recommend this page to a friend!
  Classes of Marco Palmieri   SimpleProfiler   simpleprofiler.php   Download  
File: simpleprofiler.php
Role: Class source
Content type: text/plain
Description: class
Class: SimpleProfiler
Measure the time a PHP script takes to execute
Author: By
Last change: removed extra breaklines
Date: 14 years ago
Size: 1,362 bytes
 

Contents

Class file image Download
<?php

   
class profiler {

        private
$count = 0;
        private
$time = array();

        public function
__construct() {
            return
$this->start();
        }

        public function
start() {
           
$r = false;
            if (!isset(
$this->time[0])) {
               
$this->add("start", microtime(true));
               
$r = true;
            }
            return
$r;
        }

        public function
lap($id = "lap") {
           
$r = false;
            if (isset(
$this->time[0])) {
               
$rest = $this->count == 1 ? $this->time[0]['dur'] : $this->time[0]['dur'] + $this->time[$this->count-1]['dur'];
               
$this->add($id, microtime(true) - $rest);
                if (
$id == "end") {
                   
array_shift($this->time);
                   
$r = $this->time;
                   
$this->__destruct();
                }
                else
$r = true;
            }
            return
$r;
        }

        private function
add($id, $time) {
           
$this->time[] = array("id" => $id, "dur" => $time);
           
$this->count++;
        }

        public function
stop() {
            return
$this->lap("end");
        }

        public function
__destruct() {
           
$this->count = 0;
           
$this->time = array();
        }

    }

?>