Benchmark.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Benchmark Class
00020  *
00021  * This class enables you to mark points and calculate the time difference
00022  * between them.  Memory consumption can also be displayed.
00023  *
00024  * @package             CodeIgniter
00025  * @subpackage  Libraries
00026  * @category    Libraries
00027  * @author              ExpressionEngine Dev Team
00028  * @link                http://codeigniter.com/user_guide/libraries/benchmark.html
00029  */
00030 class CI_Benchmark {
00031 
00032         var $marker = array();
00033 
00034         // --------------------------------------------------------------------
00035 
00036         /**
00037          * Set a benchmark marker
00038          *
00039          * Multiple calls to this function can be made so that several
00040          * execution points can be timed
00041          *
00042          * @access      public
00043          * @param       string  $name   name of the marker
00044          * @return      void
00045          */
00046         function mark($name)
00047         {
00048                 $this->marker[$name] = microtime();
00049         }
00050 
00051         // --------------------------------------------------------------------
00052 
00053         /**
00054          * Calculates the time difference between two marked points.
00055          *
00056          * If the first parameter is empty this function instead returns the
00057          * {elapsed_time} pseudo-variable. This permits the full system
00058          * execution time to be shown in a template. The output class will
00059          * swap the real value for this variable.
00060          *
00061          * @access      public
00062          * @param       string  a particular marked point
00063          * @param       string  a particular marked point
00064          * @param       integer the number of decimal places
00065          * @return      mixed
00066          */
00067         function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
00068         {
00069                 if ($point1 == '')
00070                 {
00071                         return '{elapsed_time}';
00072                 }
00073 
00074                 if ( ! isset($this->marker[$point1]))
00075                 {
00076                         return '';
00077                 }
00078 
00079                 if ( ! isset($this->marker[$point2]))
00080                 {
00081                         $this->marker[$point2] = microtime();
00082                 }
00083         
00084                 list($sm, $ss) = explode(' ', $this->marker[$point1]);
00085                 list($em, $es) = explode(' ', $this->marker[$point2]);
00086 
00087                 return number_format(($em + $es) - ($sm + $ss), $decimals);
00088         }
00089         
00090         // --------------------------------------------------------------------
00091 
00092         /**
00093          * Memory Usage
00094          *
00095          * This function returns the {memory_usage} pseudo-variable.
00096          * This permits it to be put it anywhere in a template
00097          * without the memory being calculated until the end.
00098          * The output class will swap the real value for this variable.
00099          *
00100          * @access      public
00101          * @return      string
00102          */
00103         function memory_usage()
00104         {
00105                 return '{memory_usage}';
00106         }
00107 
00108 }
00109 
00110 // END CI_Benchmark class
00111 
00112 /* End of file Benchmark.php */
00113 /* Location: ./system/libraries/Benchmark.php */