blob: d8dd903e766ffbebad812139eedd1d46d10559dd [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter 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
27 * @author Rick Ellis
28 * @link http://www.codeigniter.com/user_guide/libraries/benchmark.html
29 */
30class CI_Benchmark {
31
32 var $marker = array();
33
adminb0dd10f2006-08-25 17:25:49 +000034
35 // --------------------------------------------------------------------
36
37 /**
38 * Set a benchmark marker
39 *
40 * Multiple calls to this function can be made so that several
41 * execution points can be timed
42 *
43 * @access public
44 * @param string $name name of the marker
45 * @return void
46 */
47 function mark($name)
48 {
49 $this->marker[$name] = microtime();
50 }
51 // END mark()
52
53 // --------------------------------------------------------------------
54
55 /**
56 * Calculates the time difference between two marked points.
57 *
58 * If the first parameter is empty this function instead returns the
59 * {elapsed_time} pseudo-variable. This permits the the full system
60 * execution time to be shown in a template. The output class will
61 * swap the real value for this variable.
62 *
63 * @access public
64 * @param string a paricular marked point
65 * @param string a paricular marked point
66 * @param integer the number of decimal places
67 * @return mixed
68 */
69 function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
70 {
71 if ($point1 == '')
72 {
73 return '{elapsed_time}';
74 }
75
76 if ( ! isset($this->marker[$point2]))
77 $this->marker[$point2] = microtime();
78
79 list($sm, $ss) = explode(' ', $this->marker[$point1]);
80 list($em, $es) = explode(' ', $this->marker[$point2]);
81
82 return number_format(($em + $es) - ($sm + $ss), $decimals);
83 }
84 // END elapsed_time()
85
86 // --------------------------------------------------------------------
87
88 /**
89 * Memory Usage
90 *
91 * This function returns the {memory_usage} pseudo-variable.
92 * This permits it to be put it anywhere in a template
93 * without the memory being calculated until the end.
94 * The output class will swap the real value for this variable.
95 *
96 * @access public
97 * @return string
98 */
99 function memory_usage()
100 {
101 return '{memory_usage}';
102 }
103 // END memory_usage()
104
105}
106
107// END CI_Benchmark class
108?>