Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="node453.html" /> | |
13 | <link rel="prev" href="node451.html" /> | |
14 | <link rel="parent" href="profile.html" /> | |
15 | <link rel="next" href="node453.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>10.3 Instant Users Manual </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="10.2 How Is This" | |
25 | href="node451.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="10. The Python Profiler" | |
28 | href="profile.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="10.4 What Is Deterministic" | |
31 | href="node453.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="node451.html">10.2 How Is This</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="profile.html">10. The Python Profiler</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node453.html">10.4 What Is Deterministic</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0012300000000000000000"></A><A NAME="profile-instant"></A> | |
56 | <BR> | |
57 | 10.3 Instant Users Manual | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | This section is provided for users that ``don't want to read the | |
62 | manual.'' It provides a very brief overview, and allows a user to | |
63 | rapidly perform profiling on an existing application. | |
64 | ||
65 | <P> | |
66 | To profile an application with a main entry point of <tt class="function">foo()</tt>, | |
67 | you would add the following to your module: | |
68 | ||
69 | <P> | |
70 | <div class="verbatim"><pre> | |
71 | import profile | |
72 | profile.run('foo()') | |
73 | </pre></div> | |
74 | ||
75 | <P> | |
76 | The above action would cause <tt class="function">foo()</tt> to be run, and a series of | |
77 | informative lines (the profile) to be printed. The above approach is | |
78 | most useful when working with the interpreter. If you would like to | |
79 | save the results of a profile into a file for later examination, you | |
80 | can supply a file name as the second argument to the <tt class="function">run()</tt> | |
81 | function: | |
82 | ||
83 | <P> | |
84 | <div class="verbatim"><pre> | |
85 | import profile | |
86 | profile.run('foo()', 'fooprof') | |
87 | </pre></div> | |
88 | ||
89 | <P> | |
90 | The file <span class="file">profile.py</span> can also be invoked as | |
91 | a script to profile another script. For example: | |
92 | ||
93 | <P> | |
94 | <div class="verbatim"><pre> | |
95 | python -m profile myscript.py | |
96 | </pre></div> | |
97 | ||
98 | <P> | |
99 | <span class="file">profile.py</span> accepts two optional arguments on the command line: | |
100 | ||
101 | <P> | |
102 | <div class="verbatim"><pre> | |
103 | profile.py [-o output_file] [-s sort_order] | |
104 | </pre></div> | |
105 | ||
106 | <P> | |
107 | <b class="programopt">-s</b> only applies to standard output (<b class="programopt">-o</b> is | |
108 | not supplied). Look in the <tt class="class">Stats</tt> documentation for valid sort | |
109 | values. | |
110 | ||
111 | <P> | |
112 | When you wish to review the profile, you should use the methods in the | |
113 | <tt class="module">pstats</tt> module. Typically you would load the statistics data as | |
114 | follows: | |
115 | ||
116 | <P> | |
117 | <div class="verbatim"><pre> | |
118 | import pstats | |
119 | p = pstats.Stats('fooprof') | |
120 | </pre></div> | |
121 | ||
122 | <P> | |
123 | The class <tt class="class">Stats</tt> (the above code just created an instance of | |
124 | this class) has a variety of methods for manipulating and printing the | |
125 | data that was just read into <code>p</code>. When you ran | |
126 | <tt class="function">profile.run()</tt> above, what was printed was the result of three | |
127 | method calls: | |
128 | ||
129 | <P> | |
130 | <div class="verbatim"><pre> | |
131 | p.strip_dirs().sort_stats(-1).print_stats() | |
132 | </pre></div> | |
133 | ||
134 | <P> | |
135 | The first method removed the extraneous path from all the module | |
136 | names. The second method sorted all the entries according to the | |
137 | standard module/line/name string that is printed (this is to comply | |
138 | with the semantics of the old profiler). The third method printed out | |
139 | all the statistics. You might try the following sort calls: | |
140 | ||
141 | <P> | |
142 | <div class="verbatim"><pre> | |
143 | p.sort_stats('name') | |
144 | p.print_stats() | |
145 | </pre></div> | |
146 | ||
147 | <P> | |
148 | The first call will actually sort the list by function name, and the | |
149 | second call will print out the statistics. The following are some | |
150 | interesting calls to experiment with: | |
151 | ||
152 | <P> | |
153 | <div class="verbatim"><pre> | |
154 | p.sort_stats('cumulative').print_stats(10) | |
155 | </pre></div> | |
156 | ||
157 | <P> | |
158 | This sorts the profile by cumulative time in a function, and then only | |
159 | prints the ten most significant lines. If you want to understand what | |
160 | algorithms are taking time, the above line is what you would use. | |
161 | ||
162 | <P> | |
163 | If you were looking to see what functions were looping a lot, and | |
164 | taking a lot of time, you would do: | |
165 | ||
166 | <P> | |
167 | <div class="verbatim"><pre> | |
168 | p.sort_stats('time').print_stats(10) | |
169 | </pre></div> | |
170 | ||
171 | <P> | |
172 | to sort according to time spent within each function, and then print | |
173 | the statistics for the top ten functions. | |
174 | ||
175 | <P> | |
176 | You might also try: | |
177 | ||
178 | <P> | |
179 | <div class="verbatim"><pre> | |
180 | p.sort_stats('file').print_stats('__init__') | |
181 | </pre></div> | |
182 | ||
183 | <P> | |
184 | This will sort all the statistics by file name, and then print out | |
185 | statistics for only the class init methods (since they are spelled | |
186 | with <code>__init__</code> in them). As one final example, you could try: | |
187 | ||
188 | <P> | |
189 | <div class="verbatim"><pre> | |
190 | p.sort_stats('time', 'cum').print_stats(.5, 'init') | |
191 | </pre></div> | |
192 | ||
193 | <P> | |
194 | This line sorts statistics with a primary key of time, and a secondary | |
195 | key of cumulative time, and then prints out some of the statistics. | |
196 | To be specific, the list is first culled down to 50% (re: "<tt class="samp">.5</tt>") | |
197 | of its original size, then only lines containing <code>init</code> are | |
198 | maintained, and that sub-sub-list is printed. | |
199 | ||
200 | <P> | |
201 | If you wondered what functions called the above functions, you could | |
202 | now (<code>p</code> is still sorted according to the last criteria) do: | |
203 | ||
204 | <P> | |
205 | <div class="verbatim"><pre> | |
206 | p.print_callers(.5, 'init') | |
207 | </pre></div> | |
208 | ||
209 | <P> | |
210 | and you would get a list of callers for each of the listed functions. | |
211 | ||
212 | <P> | |
213 | If you want more functionality, you're going to have to read the | |
214 | manual, or guess what the following functions do: | |
215 | ||
216 | <P> | |
217 | <div class="verbatim"><pre> | |
218 | p.print_callees() | |
219 | p.add('fooprof') | |
220 | </pre></div> | |
221 | ||
222 | <P> | |
223 | Invoked as a script, the <tt class="module">pstats</tt> module is a statistics | |
224 | browser for reading and examining profile dumps. It has a simple | |
225 | line-oriented interface (implemented using <tt class="module"><a href="module-cmd.html">cmd</a></tt>) and | |
226 | interactive help. | |
227 | ||
228 | <P> | |
229 | ||
230 | <DIV CLASS="navigation"> | |
231 | <div class='online-navigation'> | |
232 | <p></p><hr /> | |
233 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
234 | <tr> | |
235 | <td class='online-navigation'><a rel="prev" title="10.2 How Is This" | |
236 | href="node451.html"><img src='../icons/previous.png' | |
237 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
238 | <td class='online-navigation'><a rel="parent" title="10. The Python Profiler" | |
239 | href="profile.html"><img src='../icons/up.png' | |
240 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
241 | <td class='online-navigation'><a rel="next" title="10.4 What Is Deterministic" | |
242 | href="node453.html"><img src='../icons/next.png' | |
243 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
244 | <td align="center" width="100%">Python Library Reference</td> | |
245 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
246 | href="contents.html"><img src='../icons/contents.png' | |
247 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
248 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
249 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
250 | <td class='online-navigation'><a rel="index" title="Index" | |
251 | href="genindex.html"><img src='../icons/index.png' | |
252 | border='0' height='32' alt='Index' width='32' /></A></td> | |
253 | </tr></table> | |
254 | <div class='online-navigation'> | |
255 | <b class="navlabel">Previous:</b> | |
256 | <a class="sectref" rel="prev" href="node451.html">10.2 How Is This</A> | |
257 | <b class="navlabel">Up:</b> | |
258 | <a class="sectref" rel="parent" href="profile.html">10. The Python Profiler</A> | |
259 | <b class="navlabel">Next:</b> | |
260 | <a class="sectref" rel="next" href="node453.html">10.4 What Is Deterministic</A> | |
261 | </div> | |
262 | </div> | |
263 | <hr /> | |
264 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
265 | </DIV> | |
266 | <!--End of Navigation Panel--> | |
267 | <ADDRESS> | |
268 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
269 | </ADDRESS> | |
270 | </BODY> | |
271 | </HTML> |