blob: 4984bb1588ff9e8c7131abde74dc2d35e7328487 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellise4a7db42008-09-12 23:35:09 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Benchmark Class
20 *
21 * This class enables you to mark points and calculate the time difference
22 * between them. Memory consumption can also be displayed.
23 *
24 * @package CodeIgniter
25 * @subpackage Libraries
26 * @category Libraries
Derek Allard3d879d52008-01-18 19:41:32 +000027 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000028 * @link http://codeigniter.com/user_guide/libraries/benchmark.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000029 */
30class CI_Benchmark {
31
32 var $marker = array();
33
34 // --------------------------------------------------------------------
35
36 /**
37 * Set a benchmark marker
38 *
39 * Multiple calls to this function can be made so that several
40 * execution points can be timed
41 *
42 * @access public
43 * @param string $name name of the marker
44 * @return void
45 */
46 function mark($name)
47 {
48 $this->marker[$name] = microtime();
49 }
Rick Ellis91a091a2008-10-01 01:07:23 +000050
Derek Allardd2df9bc2007-04-15 17:41:17 +000051 // --------------------------------------------------------------------
52
53 /**
54 * Calculates the time difference between two marked points.
55 *
56 * If the first parameter is empty this function instead returns the
57 * {elapsed_time} pseudo-variable. This permits the full system
58 * execution time to be shown in a template. The output class will
59 * swap the real value for this variable.
60 *
61 * @access public
62 * @param string a particular marked point
63 * @param string a particular marked point
64 * @param integer the number of decimal places
65 * @return mixed
66 */
67 function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
68 {
69 if ($point1 == '')
70 {
71 return '{elapsed_time}';
72 }
Derek Allard0fd8f022008-05-13 02:01:47 +000073
Derek Jones0b59f272008-05-13 04:22:33 +000074 if ( ! isset($this->marker[$point1]))
Derek Allardd2df9bc2007-04-15 17:41:17 +000075 {
76 return '';
77 }
Derek Allard0fd8f022008-05-13 02:01:47 +000078
Derek Jones0b59f272008-05-13 04:22:33 +000079 if ( ! isset($this->marker[$point2]))
Derek Allardd2df9bc2007-04-15 17:41:17 +000080 {
81 $this->marker[$point2] = microtime();
82 }
Rick Ellis91a091a2008-10-01 01:07:23 +000083
Derek Allardd2df9bc2007-04-15 17:41:17 +000084 list($sm, $ss) = explode(' ', $this->marker[$point1]);
85 list($em, $es) = explode(' ', $this->marker[$point2]);
86
87 return number_format(($em + $es) - ($sm + $ss), $decimals);
88 }
Rick Ellis91a091a2008-10-01 01:07:23 +000089
Derek Allardd2df9bc2007-04-15 17:41:17 +000090 // --------------------------------------------------------------------
91
92 /**
93 * Memory Usage
94 *
95 * This function returns the {memory_usage} pseudo-variable.
96 * This permits it to be put it anywhere in a template
97 * without the memory being calculated until the end.
98 * The output class will swap the real value for this variable.
99 *
100 * @access public
101 * @return string
102 */
103 function memory_usage()
104 {
105 return '{memory_usage}';
106 }
107
108}
109
110// END CI_Benchmark class
Derek Allard0fd8f022008-05-13 02:01:47 +0000111
112/* End of file Benchmark.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000113/* Location: ./system/libraries/Benchmark.php */