blob: cb05bfa583aa4c7cb5650e8cc42bfeac729f5b2b [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
10<script type="text/javascript" src="../scripts/nav.js"></script>
11<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
12<script type="text/javascript" src="../scripts/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
admin9fc347d2006-09-21 00:00:28 +000036<td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Benchmarking Class
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64<h1>Benchmarking Class</h1>
65
66<p>Code Igniter has a Benchmarking class that is always active, enabling the time difference between any
67two marked points to be calculated.</p>
68
69<p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
70
71
72<p>In addition, the benchmark is always started the moment the framework is
73invoked, and ended by the output class right before sending the final view to the browser, enabling a very accurate
74timing of the entire system execution to be shown.</p>
75
76<h2>Using the Benchmark</h2>
77
78<p>The Benchmark class can be used within your <a href="../general/controllers.html">controllers</a>, <a href="../general/views.html">views</a>, or your <a href="../general/models.html">Models</a>. The process for usage is this:
79
80<ol>
81<li>Mark a start point</li>
82<li>Mark an end point</li>
83<li>Run the "elapsed time" function to view the results</li>
84</ol>
85
86<p>Here's an example using real code:</p>
87
88<code>$this->benchmark->mark('start');<br />
89<br />
90// Some code happens here<br />
91<br />
92$this->benchmark->mark('end');<br />
93<br />
94echo $this->benchmark->elapsed_time('start', 'end');</code>
95
96<p><strong>Note:</strong> The words "start" and "end" are arbitrary. They are simply words used to set two markers. You can
97use any words you want, and you can set multiple sets of markers. Consider this example:</p>
98
99<code>$this->benchmark->mark('dog');<br />
100<br />
101// Some code happens here<br />
102<br />
103$this->benchmark->mark('cat');<br />
104<br />
105// More code happens here<br />
106<br />
107$this->benchmark->mark('bird');<br />
108<br />
109echo $this->benchmark->elapsed_time('dog', 'cat');<br />
110echo $this->benchmark->elapsed_time('cat', 'bird');<br />
111echo $this->benchmark->elapsed_time('dog', 'bird');</code>
112
113
114<h2>Displaying Total Execution Time</h2>
115
116<p>If you would like to display the total elapsed time from the moment Code Igniter starts to the moment the final output
117is sent to the browser, simply place this in one of your view templates:</p>
118
119<code>&lt;?=$this->benchmark->elapsed_time();?&gt;</code>
120
121<p>You'll notice that it's the same function used in the examples above to calculate the time between two point, except you are
122<strong>not</strong> using any parameters. When the parameters are absent, Code Igniter does not stop the benchmark until right before the final
123output is sent to the browser. It doesn't matter where you use the function call, the timer will continue to run until the very end.</p>
124
125<p>An alternate way to show your elapsed time in your view files is to use this pseudo-variable, if you prefer not to use the pure PHP:</p>
126<code>{elapsed_time}</code>
127
128<p class="important"><strong>Note:</strong> If you want to benchmark anything within your controller
129functions you must set your own start/end points.</p>
130
131<h2>Displaying Memory Consumption</h2>
132
133<p>If your PHP installation is configured with --enable-memory-limit, you can display the amount of memory consumed by the entire
134system using the following code in one of your view file:</p>
135
136<code>&lt;?=$this->benchmark->memory_usage();?&gt;</code>
137<p>Note: This function can only be used in your view files. The consumpiton will reflect the total memory used by the entire app.</p>
138
139<p>An alternate way to show your memory usage in your view files is to use this pseudo-variable, if you prefer not to use the pure PHP:</p>
140<code>{memory_usage}</code>
141
142
143
144</div>
145<!-- END CONTENT -->
146
147
148<div id="footer">
149<p>
150Previous Topic:&nbsp;&nbsp;<a href="../general/alternative_php.html">Alternative PHP Syntax</a>
151&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
152<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
153<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
154Next Topic:&nbsp;&nbsp;<a href="calendar.html">Calendaring Class</a>
155<p>
156<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
157</div>
158
159</body>
160</html>