PHP Classes

File: timers.inc

Recommend this page to a friend!
  Classes of Ivo Stoykov  >  timers  >  timers.inc  >  Download  
File: timers.inc
Role: ???
Content type: text/plain
Description: timer class file
Class: timers
Author: By
Last change:
Date: 20 years ago
Size: 4,373 bytes
 

Contents

Class file image Download
<?php
/******************************************************************************
*    timer v1.0
*    Copyright (C) 2002 Ivo Stoykov (ivostoykov@hotmail.com)
*
*    This class was inspired by:
*        Nathan Wallace's article PHP: Hackers Paradise
*              (http://www.e-gineer.com/articles/php-hackers-paradise.phtml )
*    and
*        Allan Kent's article Timing Script Execution
*              (sorry but I've lost the url')
*
*    This script is freeware and is realeased under the GPL.
*    (see attached file GPL.txt)
*    It is distributed in the hope that it will be useful
*    but WITHOUT ANY WARRANTY.
*
*    You can redistribute it and/or modify it under the
*    terms of the license (attached as GPL.txt)
*
*    If you did not receive a copy of the GNU General Public License
*    along with this program you could obtain a copy eithrt by writing to
*    the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*    Or by downloading it from http://fsf.org/
********************************************************************************
*    This script is to measure different proceces in your web site.
*
*    You could combine it with logger class so as to be informed. (i.e. how long
*    users has stay on a particular page)
********************************************************************************/
/*
   CLASS:   timer
   PURPOSE: compute the time elapsed for a given process (i.e. showing a web page into a browser)
   USAGE:   1. include the class file - i.e. include('timer.inc');
            2. start the timer. (i.e. $the_time = new timer(); )
            3. compute the time elapsed. (i.e. print $the_time->get_elapsed_time(); )

   for more info see timerReadMe.txt
*/

// prevent double loading the class module
if ( defined( 'TIMERS_CLASS' ) ) return;
define ('TIMERS_CLASS', 'timers.inc', 1);

class timer {
// global vars
   var $timer;

function timer($name = "") {
   $this->reset_timer($name);
   if(isset($name) && $name != "") {
      $this->timer[$name] = array('start' => -1, 'end' => -1, 'total_time' => -1);
      $this->start_timer($name);
   }  // end if isset block
}
/*
      start the subsequent time
      RETURN      true if created; false otherwise
      PARAMETERS: $name of the timer - could be any valid string
*/
   function start_timer($name = "") {
      $this->timer[$name]['start'] = $this->set_timer();
   }

// what is this for ?!? ;)
   function stop_timer($name = "") {
      if(!isset($this->timer[$name]['end']) || $this->timer[$name]['end'] == -1) {
         $this->timer[$name]['end'] = $this->set_timer();
      }
      $this->timer[$name]['total_time'] = $this->get_end_time($name) - $this->get_start_time($name);
   }

// if timer stopped by mistake get new _END_ time here
   function stretch_timer($name = "") {
      $this->timer[$name]['end'] = $this->set_timer();
      $this->timer[$name]['total_time'] = $this->get_end_time($name) - $this->get_start_time($name);
   }

// set functions
   function set_timer() {
      list($usec, $sec) = explode(" ",microtime());
      return ((float)$usec + (float)$sec);
   }  // end function

// get functions
   function get_start_time($name = "")    {
      if($this->timer[$name]['start'] == -1) {
         $this->start_timer($name);
      }  // end if isset blockset
      return $this->timer[$name]['start'];
   }  // end function

   function get_end_time($name = "") {
      if(!isset($this->timer[$name]['end']) ||
          $this->timer[$name]['end'] == -1) {
         $this->stop_timer($name);
      }
      return $this->timer[$name]['end'];
   }  // end function

   function get_elapsed_time($name = "") {
      if(!isset($this->timer[$name]['total_time']) ||
          $this->timer[$name]['total_time'] == -1)  {
        $this->stop_timer($name);
      }
      return $this->timer[$name]['total_time'];
   }  // end function

// other functions
   function reset_timer($name = "") {
      if(isset($this->timer[$name])) {
         unset($this->timer[$name]);
      }  // end if isset block
   }  // end function

   function reset_all_timers() {
      if(isset($this->timer)) {
         unset($this->timer);
      }  // end if isset block
   }  // end function

}  // end of class
?>
For more information send a message to info at phpclasses dot org.