Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 1 | \chapter{The Python Profiler \label{profile}} |
| 2 | |
| 3 | \sectionauthor{James Roskind}{} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 4 | |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 5 | Copyright \copyright{} 1994, by InfoSeek Corporation, all rights reserved. |
Fred Drake | 5dabeed | 1998-04-03 07:02:35 +0000 | [diff] [blame] | 6 | \index{InfoSeek Corporation} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 7 | |
Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 8 | Written by James Roskind.\footnote{ |
| 9 | Updated and converted to \LaTeX\ by Guido van Rossum. The references to |
| 10 | the old profiler are left in the text, although it no longer exists.} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 11 | |
| 12 | Permission to use, copy, modify, and distribute this Python software |
| 13 | and its associated documentation for any purpose (subject to the |
| 14 | restriction in the following sentence) without fee is hereby granted, |
| 15 | provided that the above copyright notice appears in all copies, and |
| 16 | that both that copyright notice and this permission notice appear in |
| 17 | supporting documentation, and that the name of InfoSeek not be used in |
| 18 | advertising or publicity pertaining to distribution of the software |
| 19 | without specific, written prior permission. This permission is |
| 20 | explicitly restricted to the copying and modification of the software |
| 21 | to remain in Python, compiled Python, or other languages (such as C) |
| 22 | wherein the modified or derived code is exclusively imported into a |
| 23 | Python module. |
| 24 | |
| 25 | INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
| 26 | SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 27 | FITNESS. IN NO EVENT SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY |
| 28 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 29 | RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 30 | CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 31 | CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 32 | |
| 33 | |
| 34 | The profiler was written after only programming in Python for 3 weeks. |
| 35 | As a result, it is probably clumsy code, but I don't know for sure yet |
| 36 | 'cause I'm a beginner :-). I did work hard to make the code run fast, |
| 37 | so that profiling would be a reasonable thing to do. I tried not to |
| 38 | repeat code fragments, but I'm sure I did some stuff in really awkward |
| 39 | ways at times. Please send suggestions for improvements to: |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 40 | \email{jar@netscape.com}. I won't promise \emph{any} support. ...but |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 41 | I'd appreciate the feedback. |
| 42 | |
| 43 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 44 | \section{Introduction to the profiler} |
Guido van Rossum | 86cb092 | 1995-03-20 12:59:56 +0000 | [diff] [blame] | 45 | \nodename{Profiler Introduction} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 46 | |
| 47 | A \dfn{profiler} is a program that describes the run time performance |
| 48 | of a program, providing a variety of statistics. This documentation |
| 49 | describes the profiler functionality provided in the modules |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 50 | \module{profile} and \module{pstats}. This profiler provides |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 51 | \dfn{deterministic profiling} of any Python programs. It also |
| 52 | provides a series of report generation tools to allow users to rapidly |
| 53 | examine the results of a profile operation. |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 54 | \index{deterministic profiling} |
| 55 | \index{profiling, deterministic} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | \section{How Is This Profiler Different From The Old Profiler?} |
Guido van Rossum | 86cb092 | 1995-03-20 12:59:56 +0000 | [diff] [blame] | 59 | \nodename{Profiler Changes} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 364e643 | 1997-11-18 15:28:46 +0000 | [diff] [blame] | 61 | (This section is of historical importance only; the old profiler |
| 62 | discussed here was last seen in Python 1.1.) |
| 63 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 64 | The big changes from old profiling module are that you get more |
| 65 | information, and you pay less CPU time. It's not a trade-off, it's a |
| 66 | trade-up. |
| 67 | |
| 68 | To be specific: |
| 69 | |
| 70 | \begin{description} |
| 71 | |
| 72 | \item[Bugs removed:] |
| 73 | Local stack frame is no longer molested, execution time is now charged |
| 74 | to correct functions. |
| 75 | |
| 76 | \item[Accuracy increased:] |
| 77 | Profiler execution time is no longer charged to user's code, |
| 78 | calibration for platform is supported, file reads are not done \emph{by} |
| 79 | profiler \emph{during} profiling (and charged to user's code!). |
| 80 | |
| 81 | \item[Speed increased:] |
| 82 | Overhead CPU cost was reduced by more than a factor of two (perhaps a |
| 83 | factor of five), lightweight profiler module is all that must be |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 84 | loaded, and the report generating module (\module{pstats}) is not needed |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 85 | during profiling. |
| 86 | |
| 87 | \item[Recursive functions support:] |
| 88 | Cumulative times in recursive functions are correctly calculated; |
| 89 | recursive entries are counted. |
| 90 | |
| 91 | \item[Large growth in report generating UI:] |
| 92 | Distinct profiles runs can be added together forming a comprehensive |
| 93 | report; functions that import statistics take arbitrary lists of |
| 94 | files; sorting criteria is now based on keywords (instead of 4 integer |
| 95 | options); reports shows what functions were profiled as well as what |
| 96 | profile file was referenced; output format has been improved. |
| 97 | |
| 98 | \end{description} |
| 99 | |
| 100 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 101 | \section{Instant Users Manual \label{profile-instant}} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 102 | |
| 103 | This section is provided for users that ``don't want to read the |
| 104 | manual.'' It provides a very brief overview, and allows a user to |
| 105 | rapidly perform profiling on an existing application. |
| 106 | |
| 107 | To profile an application with a main entry point of \samp{foo()}, you |
| 108 | would add the following to your module: |
| 109 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 110 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 111 | import profile |
Fred Drake | 2cb824c | 1998-04-09 18:10:35 +0000 | [diff] [blame] | 112 | profile.run('foo()') |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 113 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 115 | The above action would cause \samp{foo()} to be run, and a series of |
| 116 | informative lines (the profile) to be printed. The above approach is |
| 117 | most useful when working with the interpreter. If you would like to |
| 118 | save the results of a profile into a file for later examination, you |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 119 | can supply a file name as the second argument to the \function{run()} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 120 | function: |
| 121 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 122 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 123 | import profile |
Fred Drake | 2cb824c | 1998-04-09 18:10:35 +0000 | [diff] [blame] | 124 | profile.run('foo()', 'fooprof') |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 125 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 126 | |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 127 | The file \file{profile.py} can also be invoked as |
Guido van Rossum | bac8002 | 1997-06-02 17:29:12 +0000 | [diff] [blame] | 128 | a script to profile another script. For example: |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 129 | |
| 130 | \begin{verbatim} |
Fred Drake | 5dabeed | 1998-04-03 07:02:35 +0000 | [diff] [blame] | 131 | python /usr/local/lib/python1.5/profile.py myscript.py |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 132 | \end{verbatim} |
Guido van Rossum | bac8002 | 1997-06-02 17:29:12 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 134 | When you wish to review the profile, you should use the methods in the |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 135 | \module{pstats} module. Typically you would load the statistics data as |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 136 | follows: |
| 137 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 138 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 139 | import pstats |
| 140 | p = pstats.Stats('fooprof') |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 141 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 142 | |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 143 | The class \class{Stats} (the above code just created an instance of |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 144 | this class) has a variety of methods for manipulating and printing the |
| 145 | data that was just read into \samp{p}. When you ran |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 146 | \function{profile.run()} above, what was printed was the result of three |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 147 | method calls: |
| 148 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 149 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 150 | p.strip_dirs().sort_stats(-1).print_stats() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 151 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 153 | The first method removed the extraneous path from all the module |
| 154 | names. The second method sorted all the entries according to the |
| 155 | standard module/line/name string that is printed (this is to comply |
| 156 | with the semantics of the old profiler). The third method printed out |
| 157 | all the statistics. You might try the following sort calls: |
| 158 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 159 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 160 | p.sort_stats('name') |
| 161 | p.print_stats() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 162 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 164 | The first call will actually sort the list by function name, and the |
| 165 | second call will print out the statistics. The following are some |
| 166 | interesting calls to experiment with: |
| 167 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 168 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 169 | p.sort_stats('cumulative').print_stats(10) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 170 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 172 | This sorts the profile by cumulative time in a function, and then only |
| 173 | prints the ten most significant lines. If you want to understand what |
| 174 | algorithms are taking time, the above line is what you would use. |
| 175 | |
| 176 | If you were looking to see what functions were looping a lot, and |
| 177 | taking a lot of time, you would do: |
| 178 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 179 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 180 | p.sort_stats('time').print_stats(10) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 181 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 183 | to sort according to time spent within each function, and then print |
| 184 | the statistics for the top ten functions. |
| 185 | |
| 186 | You might also try: |
| 187 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 188 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 189 | p.sort_stats('file').print_stats('__init__') |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 190 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 192 | This will sort all the statistics by file name, and then print out |
| 193 | statistics for only the class init methods ('cause they are spelled |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 194 | with \samp{__init__} in them). As one final example, you could try: |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 195 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 196 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 197 | p.sort_stats('time', 'cum').print_stats(.5, 'init') |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 198 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 200 | This line sorts statistics with a primary key of time, and a secondary |
| 201 | key of cumulative time, and then prints out some of the statistics. |
| 202 | To be specific, the list is first culled down to 50\% (re: \samp{.5}) |
| 203 | of its original size, then only lines containing \code{init} are |
| 204 | maintained, and that sub-sub-list is printed. |
| 205 | |
| 206 | If you wondered what functions called the above functions, you could |
| 207 | now (\samp{p} is still sorted according to the last criteria) do: |
| 208 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 209 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 210 | p.print_callers(.5, 'init') |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 211 | \end{verbatim} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 212 | |
Tim Peters | 0a1fc4e | 2001-10-07 03:12:08 +0000 | [diff] [blame] | 213 | and you would get a list of callers for each of the listed functions. |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 214 | |
| 215 | If you want more functionality, you're going to have to read the |
| 216 | manual, or guess what the following functions do: |
| 217 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 218 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 219 | p.print_callees() |
| 220 | p.add('fooprof') |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 221 | \end{verbatim} |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 222 | |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 223 | Invoked as a script, the \module{pstats} module is a statistics |
| 224 | browser for reading and examining profile dumps. It has a simple |
Fred Drake | a3e56a6 | 2001-04-13 14:34:58 +0000 | [diff] [blame] | 225 | line-oriented interface (implemented using \refmodule{cmd}) and |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 226 | interactive help. |
| 227 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 228 | \section{What Is Deterministic Profiling?} |
Guido van Rossum | 86cb092 | 1995-03-20 12:59:56 +0000 | [diff] [blame] | 229 | \nodename{Deterministic Profiling} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 230 | |
| 231 | \dfn{Deterministic profiling} is meant to reflect the fact that all |
Fred Drake | a3e56a6 | 2001-04-13 14:34:58 +0000 | [diff] [blame] | 232 | \emph{function call}, \emph{function return}, and \emph{exception} events |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 233 | are monitored, and precise timings are made for the intervals between |
| 234 | these events (during which time the user's code is executing). In |
| 235 | contrast, \dfn{statistical profiling} (which is not done by this |
| 236 | module) randomly samples the effective instruction pointer, and |
| 237 | deduces where time is being spent. The latter technique traditionally |
| 238 | involves less overhead (as the code does not need to be instrumented), |
| 239 | but provides only relative indications of where time is being spent. |
| 240 | |
| 241 | In Python, since there is an interpreter active during execution, the |
| 242 | presence of instrumented code is not required to do deterministic |
| 243 | profiling. Python automatically provides a \dfn{hook} (optional |
| 244 | callback) for each event. In addition, the interpreted nature of |
| 245 | Python tends to add so much overhead to execution, that deterministic |
| 246 | profiling tends to only add small processing overhead in typical |
| 247 | applications. The result is that deterministic profiling is not that |
| 248 | expensive, yet provides extensive run time statistics about the |
| 249 | execution of a Python program. |
| 250 | |
| 251 | Call count statistics can be used to identify bugs in code (surprising |
| 252 | counts), and to identify possible inline-expansion points (high call |
| 253 | counts). Internal time statistics can be used to identify ``hot |
| 254 | loops'' that should be carefully optimized. Cumulative time |
| 255 | statistics should be used to identify high level errors in the |
| 256 | selection of algorithms. Note that the unusual handling of cumulative |
| 257 | times in this profiler allows statistics for recursive implementations |
| 258 | of algorithms to be directly compared to iterative implementations. |
| 259 | |
| 260 | |
| 261 | \section{Reference Manual} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 262 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 263 | \declaremodule{standard}{profile} |
| 264 | \modulesynopsis{Python profiler} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 267 | |
| 268 | The primary entry point for the profiler is the global function |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 269 | \function{profile.run()}. It is typically used to create any profile |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 270 | information. The reports are formatted and printed using methods of |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 271 | the class \class{pstats.Stats}. The following is a description of all |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 272 | of these standard entry points and functions. For a more in-depth |
| 273 | view of some of the code, consider reading the later section on |
| 274 | Profiler Extensions, which includes discussion of how to derive |
| 275 | ``better'' profilers from the classes presented, or reading the source |
| 276 | code for these modules. |
| 277 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 278 | \begin{funcdesc}{run}{string\optional{, filename\optional{, ...}}} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 279 | |
| 280 | This function takes a single argument that has can be passed to the |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 281 | \keyword{exec} statement, and an optional file name. In all cases this |
| 282 | routine attempts to \keyword{exec} its first argument, and gather profiling |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 283 | statistics from the execution. If no file name is present, then this |
| 284 | function automatically prints a simple profiling report, sorted by the |
| 285 | standard name string (file/line/function-name) that is presented in |
| 286 | each line. The following is a typical output from such a call: |
| 287 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 288 | \begin{verbatim} |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 289 | main() |
| 290 | 2706 function calls (2004 primitive calls) in 4.504 CPU seconds |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 292 | Ordered by: standard name |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 294 | ncalls tottime percall cumtime percall filename:lineno(function) |
| 295 | 2 0.006 0.003 0.953 0.477 pobject.py:75(save_objects) |
| 296 | 43/3 0.533 0.012 0.749 0.250 pobject.py:99(evaluate) |
| 297 | ... |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 298 | \end{verbatim} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 299 | |
| 300 | The first line indicates that this profile was generated by the call:\\ |
| 301 | \code{profile.run('main()')}, and hence the exec'ed string is |
| 302 | \code{'main()'}. The second line indicates that 2706 calls were |
| 303 | monitored. Of those calls, 2004 were \dfn{primitive}. We define |
| 304 | \dfn{primitive} to mean that the call was not induced via recursion. |
| 305 | The next line: \code{Ordered by:\ standard name}, indicates that |
| 306 | the text string in the far right column was used to sort the output. |
| 307 | The column headings include: |
| 308 | |
| 309 | \begin{description} |
| 310 | |
| 311 | \item[ncalls ] |
Tim Peters | 0a1fc4e | 2001-10-07 03:12:08 +0000 | [diff] [blame] | 312 | for the number of calls, |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 313 | |
| 314 | \item[tottime ] |
| 315 | for the total time spent in the given function (and excluding time |
| 316 | made in calls to sub-functions), |
| 317 | |
| 318 | \item[percall ] |
| 319 | is the quotient of \code{tottime} divided by \code{ncalls} |
| 320 | |
| 321 | \item[cumtime ] |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 322 | is the total time spent in this and all subfunctions (from invocation |
| 323 | till exit). This figure is accurate \emph{even} for recursive |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 324 | functions. |
| 325 | |
| 326 | \item[percall ] |
| 327 | is the quotient of \code{cumtime} divided by primitive calls |
| 328 | |
| 329 | \item[filename:lineno(function) ] |
| 330 | provides the respective data of each function |
| 331 | |
| 332 | \end{description} |
| 333 | |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 334 | When there are two numbers in the first column (for example, |
| 335 | \samp{43/3}), then the latter is the number of primitive calls, and |
| 336 | the former is the actual number of calls. Note that when the function |
| 337 | does not recurse, these two values are the same, and only the single |
| 338 | figure is printed. |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 340 | \end{funcdesc} |
| 341 | |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 342 | Analysis of the profiler data is done using this class from the |
| 343 | \module{pstats} module: |
| 344 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 345 | % now switch modules.... |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 346 | % (This \stmodindex use may be hard to change ;-( ) |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 347 | \stmodindex{pstats} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 348 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 349 | \begin{classdesc}{Stats}{filename\optional{, ...}} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 350 | This class constructor creates an instance of a ``statistics object'' |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 351 | from a \var{filename} (or set of filenames). \class{Stats} objects are |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 352 | manipulated by methods, in order to print useful reports. |
| 353 | |
| 354 | The file selected by the above constructor must have been created by |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 355 | the corresponding version of \module{profile}. To be specific, there is |
| 356 | \emph{no} file compatibility guaranteed with future versions of this |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 357 | profiler, and there is no compatibility with files produced by other |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 358 | profilers (such as the old system profiler). |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 359 | |
| 360 | If several files are provided, all the statistics for identical |
| 361 | functions will be coalesced, so that an overall view of several |
| 362 | processes can be considered in a single report. If additional files |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 363 | need to be combined with data in an existing \class{Stats} object, the |
| 364 | \method{add()} method can be used. |
| 365 | \end{classdesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 366 | |
| 367 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 368 | \subsection{The \class{Stats} Class \label{profile-stats}} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 369 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 370 | \class{Stats} objects have the following methods: |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 371 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 372 | \begin{methoddesc}[Stats]{strip_dirs}{} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 373 | This method for the \class{Stats} class removes all leading path |
| 374 | information from file names. It is very useful in reducing the size |
| 375 | of the printout to fit within (close to) 80 columns. This method |
| 376 | modifies the object, and the stripped information is lost. After |
| 377 | performing a strip operation, the object is considered to have its |
| 378 | entries in a ``random'' order, as it was just after object |
| 379 | initialization and loading. If \method{strip_dirs()} causes two |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 380 | function names to be indistinguishable (they are on the same |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 381 | line of the same filename, and have the same function name), then the |
| 382 | statistics for these two entries are accumulated into a single entry. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 383 | \end{methoddesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 384 | |
| 385 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 386 | \begin{methoddesc}[Stats]{add}{filename\optional{, ...}} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 387 | This method of the \class{Stats} class accumulates additional |
| 388 | profiling information into the current profiling object. Its |
| 389 | arguments should refer to filenames created by the corresponding |
| 390 | version of \function{profile.run()}. Statistics for identically named |
| 391 | (re: file, line, name) functions are automatically accumulated into |
| 392 | single function statistics. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 393 | \end{methoddesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 394 | |
Fred Drake | 126d366 | 2003-05-14 14:29:27 +0000 | [diff] [blame] | 395 | \begin{methoddesc}[Stats]{dump_stats}{filename} |
| 396 | Save the data loaded into the \class{Stats} object to a file named |
| 397 | \var{filename}. The file is created if it does not exist, and is |
| 398 | overwritten if it already exists. This is equivalent to the method of |
| 399 | the same name on the \class{profile.Profile} class. |
| 400 | \versionadded{2.3} |
| 401 | \end{methoddesc} |
| 402 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 403 | \begin{methoddesc}[Stats]{sort_stats}{key\optional{, ...}} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 404 | This method modifies the \class{Stats} object by sorting it according |
| 405 | to the supplied criteria. The argument is typically a string |
Fred Drake | 2cb824c | 1998-04-09 18:10:35 +0000 | [diff] [blame] | 406 | identifying the basis of a sort (example: \code{'time'} or |
| 407 | \code{'name'}). |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 408 | |
| 409 | When more than one key is provided, then additional keys are used as |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 410 | secondary criteria when there is equality in all keys selected |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 411 | before them. For example, \samp{sort_stats('name', 'file')} will sort |
| 412 | all the entries according to their function name, and resolve all ties |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 413 | (identical function names) by sorting by file name. |
| 414 | |
| 415 | Abbreviations can be used for any key names, as long as the |
| 416 | abbreviation is unambiguous. The following are the keys currently |
Tim Peters | 0a1fc4e | 2001-10-07 03:12:08 +0000 | [diff] [blame] | 417 | defined: |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 418 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 419 | \begin{tableii}{l|l}{code}{Valid Arg}{Meaning} |
Fred Drake | 5dabeed | 1998-04-03 07:02:35 +0000 | [diff] [blame] | 420 | \lineii{'calls'}{call count} |
| 421 | \lineii{'cumulative'}{cumulative time} |
| 422 | \lineii{'file'}{file name} |
| 423 | \lineii{'module'}{file name} |
| 424 | \lineii{'pcalls'}{primitive call count} |
| 425 | \lineii{'line'}{line number} |
| 426 | \lineii{'name'}{function name} |
| 427 | \lineii{'nfl'}{name/file/line} |
| 428 | \lineii{'stdname'}{standard name} |
| 429 | \lineii{'time'}{internal time} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 430 | \end{tableii} |
| 431 | |
| 432 | Note that all sorts on statistics are in descending order (placing |
| 433 | most time consuming items first), where as name, file, and line number |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 434 | searches are in ascending order (alphabetical). The subtle |
Fred Drake | 2cb824c | 1998-04-09 18:10:35 +0000 | [diff] [blame] | 435 | distinction between \code{'nfl'} and \code{'stdname'} is that the |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 436 | standard name is a sort of the name as printed, which means that the |
| 437 | embedded line numbers get compared in an odd way. For example, lines |
| 438 | 3, 20, and 40 would (if the file names were the same) appear in the |
Fred Drake | 2cb824c | 1998-04-09 18:10:35 +0000 | [diff] [blame] | 439 | string order 20, 3 and 40. In contrast, \code{'nfl'} does a numeric |
| 440 | compare of the line numbers. In fact, \code{sort_stats('nfl')} is the |
| 441 | same as \code{sort_stats('name', 'file', 'line')}. |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 442 | |
| 443 | For compatibility with the old profiler, the numeric arguments |
Fred Drake | 2cb824c | 1998-04-09 18:10:35 +0000 | [diff] [blame] | 444 | \code{-1}, \code{0}, \code{1}, and \code{2} are permitted. They are |
| 445 | interpreted as \code{'stdname'}, \code{'calls'}, \code{'time'}, and |
| 446 | \code{'cumulative'} respectively. If this old style format (numeric) |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 447 | is used, only one sort key (the numeric key) will be used, and |
| 448 | additional arguments will be silently ignored. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 449 | \end{methoddesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 450 | |
| 451 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 452 | \begin{methoddesc}[Stats]{reverse_order}{} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 453 | This method for the \class{Stats} class reverses the ordering of the basic |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 454 | list within the object. This method is provided primarily for |
| 455 | compatibility with the old profiler. Its utility is questionable |
| 456 | now that ascending vs descending order is properly selected based on |
| 457 | the sort key of choice. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 458 | \end{methoddesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 459 | |
Fred Drake | 20006b2 | 2001-07-02 21:22:39 +0000 | [diff] [blame] | 460 | \begin{methoddesc}[Stats]{print_stats}{\optional{restriction, \moreargs}} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 461 | This method for the \class{Stats} class prints out a report as described |
| 462 | in the \function{profile.run()} definition. |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 463 | |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 464 | The order of the printing is based on the last \method{sort_stats()} |
| 465 | operation done on the object (subject to caveats in \method{add()} and |
Raymond Hettinger | 0e53d23 | 2003-07-14 18:24:26 +0000 | [diff] [blame] | 466 | \method{strip_dirs()}). |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 467 | |
| 468 | The arguments provided (if any) can be used to limit the list down to |
| 469 | the significant entries. Initially, the list is taken to be the |
| 470 | complete set of profiled functions. Each restriction is either an |
| 471 | integer (to select a count of lines), or a decimal fraction between |
| 472 | 0.0 and 1.0 inclusive (to select a percentage of lines), or a regular |
Guido van Rossum | 364e643 | 1997-11-18 15:28:46 +0000 | [diff] [blame] | 473 | expression (to pattern match the standard name that is printed; as of |
| 474 | Python 1.5b1, this uses the Perl-style regular expression syntax |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 475 | defined by the \refmodule{re} module). If several restrictions are |
Guido van Rossum | 364e643 | 1997-11-18 15:28:46 +0000 | [diff] [blame] | 476 | provided, then they are applied sequentially. For example: |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 477 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 478 | \begin{verbatim} |
Fred Drake | 2cb824c | 1998-04-09 18:10:35 +0000 | [diff] [blame] | 479 | print_stats(.1, 'foo:') |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 480 | \end{verbatim} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 481 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 482 | would first limit the printing to first 10\% of list, and then only |
| 483 | print functions that were part of filename \samp{.*foo:}. In |
| 484 | contrast, the command: |
| 485 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 486 | \begin{verbatim} |
Fred Drake | 2cb824c | 1998-04-09 18:10:35 +0000 | [diff] [blame] | 487 | print_stats('foo:', .1) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 488 | \end{verbatim} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 489 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 490 | would limit the list to all functions having file names \samp{.*foo:}, |
| 491 | and then proceed to only print the first 10\% of them. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 492 | \end{methoddesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 493 | |
| 494 | |
Fred Drake | 20006b2 | 2001-07-02 21:22:39 +0000 | [diff] [blame] | 495 | \begin{methoddesc}[Stats]{print_callers}{\optional{restriction, \moreargs}} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 496 | This method for the \class{Stats} class prints a list of all functions |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 497 | that called each function in the profiled database. The ordering is |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 498 | identical to that provided by \method{print_stats()}, and the definition |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 499 | of the restricting argument is also identical. For convenience, a |
| 500 | number is shown in parentheses after each caller to show how many |
| 501 | times this specific call was made. A second non-parenthesized number |
| 502 | is the cumulative time spent in the function at the right. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 503 | \end{methoddesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 504 | |
Fred Drake | 20006b2 | 2001-07-02 21:22:39 +0000 | [diff] [blame] | 505 | \begin{methoddesc}[Stats]{print_callees}{\optional{restriction, \moreargs}} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 506 | This method for the \class{Stats} class prints a list of all function |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 507 | that were called by the indicated function. Aside from this reversal |
| 508 | of direction of calls (re: called vs was called by), the arguments and |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 509 | ordering are identical to the \method{print_callers()} method. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 510 | \end{methoddesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 511 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 512 | \begin{methoddesc}[Stats]{ignore}{} |
Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 513 | \deprecated{1.5.1}{This is not needed in modern versions of |
| 514 | Python.\footnote{ |
| 515 | This was once necessary, when Python would print any unused expression |
| 516 | result that was not \code{None}. The method is still defined for |
| 517 | backward compatibility.}} |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 518 | \end{methoddesc} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 519 | |
| 520 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 521 | \section{Limitations \label{profile-limits}} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 522 | |
| 523 | There are two fundamental limitations on this profiler. The first is |
| 524 | that it relies on the Python interpreter to dispatch \dfn{call}, |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 525 | \dfn{return}, and \dfn{exception} events. Compiled \C{} code does not |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 526 | get interpreted, and hence is ``invisible'' to the profiler. All time |
Fred Drake | 3a18f3b | 1998-04-02 19:36:25 +0000 | [diff] [blame] | 527 | spent in \C{} code (including built-in functions) will be charged to the |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 528 | Python function that invoked the \C{} code. If the \C{} code calls out |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 529 | to some native Python code, then those calls will be profiled |
| 530 | properly. |
| 531 | |
| 532 | The second limitation has to do with accuracy of timing information. |
| 533 | There is a fundamental problem with deterministic profilers involving |
| 534 | accuracy. The most obvious restriction is that the underlying ``clock'' |
| 535 | is only ticking at a rate (typically) of about .001 seconds. Hence no |
Raymond Hettinger | 999b57c | 2003-08-25 04:28:05 +0000 | [diff] [blame] | 536 | measurements will be more accurate than the underlying clock. If |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 537 | enough measurements are taken, then the ``error'' will tend to average |
| 538 | out. Unfortunately, removing this first error induces a second source |
| 539 | of error... |
| 540 | |
| 541 | The second problem is that it ``takes a while'' from when an event is |
| 542 | dispatched until the profiler's call to get the time actually |
| 543 | \emph{gets} the state of the clock. Similarly, there is a certain lag |
| 544 | when exiting the profiler event handler from the time that the clock's |
| 545 | value was obtained (and then squirreled away), until the user's code |
| 546 | is once again executing. As a result, functions that are called many |
| 547 | times, or call many functions, will typically accumulate this error. |
| 548 | The error that accumulates in this fashion is typically less than the |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 549 | accuracy of the clock (less than one clock tick), but it |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 550 | \emph{can} accumulate and become very significant. This profiler |
| 551 | provides a means of calibrating itself for a given platform so that |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 552 | this error can be probabilistically (on the average) removed. |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 553 | After the profiler is calibrated, it will be more accurate (in a least |
| 554 | square sense), but it will sometimes produce negative numbers (when |
| 555 | call counts are exceptionally low, and the gods of probability work |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 556 | against you :-). ) Do \emph{not} be alarmed by negative numbers in |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 557 | the profile. They should \emph{only} appear if you have calibrated |
| 558 | your profiler, and the results are actually better than without |
| 559 | calibration. |
| 560 | |
| 561 | |
Fred Drake | b9f1f6d | 1999-04-21 21:43:17 +0000 | [diff] [blame] | 562 | \section{Calibration \label{profile-calibration}} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 563 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 564 | The profiler subtracts a constant from each |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 565 | event handling time to compensate for the overhead of calling the time |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 566 | function, and socking away the results. By default, the constant is 0. |
| 567 | The following procedure can |
| 568 | be used to obtain a better constant for a given platform (see discussion |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 569 | in section Limitations above). |
| 570 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 571 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 572 | import profile |
| 573 | pr = profile.Profile() |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 574 | for i in range(5): |
| 575 | print pr.calibrate(10000) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 576 | \end{verbatim} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 577 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 578 | The method executes the number of Python calls given by the argument, |
| 579 | directly and again under the profiler, measuring the time for both. |
| 580 | It then computes the hidden overhead per profiler event, and returns |
| 581 | that as a float. For example, on an 800 MHz Pentium running |
| 582 | Windows 2000, and using Python's time.clock() as the timer, |
| 583 | the magical number is about 12.5e-6. |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 584 | |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 585 | The object of this exercise is to get a fairly consistent result. |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 586 | If your computer is \emph{very} fast, or your timer function has poor |
| 587 | resolution, you might have to pass 100000, or even 1000000, to get |
| 588 | consistent results. |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 589 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 590 | When you have a consistent answer, |
| 591 | there are three ways you can use it:\footnote{Prior to Python 2.2, it |
| 592 | was necessary to edit the profiler source code to embed the bias as |
| 593 | a literal number. You still can, but that method is no longer |
| 594 | described, because no longer needed.} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 595 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 596 | \begin{verbatim} |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 597 | import profile |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 598 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 599 | # 1. Apply computed bias to all Profile instances created hereafter. |
Tim Peters | 8cd015c | 2001-10-09 20:54:23 +0000 | [diff] [blame] | 600 | profile.Profile.bias = your_computed_bias |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 601 | |
| 602 | # 2. Apply computed bias to a specific Profile instance. |
| 603 | pr = profile.Profile() |
| 604 | pr.bias = your_computed_bias |
| 605 | |
| 606 | # 3. Specify computed bias in instance constructor. |
| 607 | pr = profile.Profile(bias=your_computed_bias) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 608 | \end{verbatim} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 609 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 610 | If you have a choice, you are better off choosing a smaller constant, and |
| 611 | then your results will ``less often'' show up as negative in profile |
| 612 | statistics. |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 613 | |
| 614 | |
Guido van Rossum | 86cb092 | 1995-03-20 12:59:56 +0000 | [diff] [blame] | 615 | \section{Extensions --- Deriving Better Profilers} |
| 616 | \nodename{Profiler Extensions} |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 617 | |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 618 | The \class{Profile} class of module \module{profile} was written so that |
Tim Peters | 0a1fc4e | 2001-10-07 03:12:08 +0000 | [diff] [blame] | 619 | derived classes could be developed to extend the profiler. The details |
| 620 | are not described here, as doing this successfully requires an expert |
| 621 | understanding of how the \class{Profile} class works internally. Study |
| 622 | the source code of module \module{profile} carefully if you want to |
| 623 | pursue this. |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 624 | |
Tim Peters | 0a1fc4e | 2001-10-07 03:12:08 +0000 | [diff] [blame] | 625 | If all you want to do is change how current time is determined (for |
| 626 | example, to force use of wall-clock time or elapsed process time), |
| 627 | pass the timing function you want to the \class{Profile} class |
| 628 | constructor: |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 629 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 630 | \begin{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 631 | pr = profile.Profile(your_time_func) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 632 | \end{verbatim} |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 633 | |
Tim Peters | 0a1fc4e | 2001-10-07 03:12:08 +0000 | [diff] [blame] | 634 | The resulting profiler will then call \code{your_time_func()}. |
| 635 | The function should return a single number, or a list of |
| 636 | numbers whose sum is the current time (like what \function{os.times()} |
| 637 | returns). If the function returns a single time number, or the list of |
| 638 | returned numbers has length 2, then you will get an especially fast |
| 639 | version of the dispatch routine. |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 640 | |
Tim Peters | 0a1fc4e | 2001-10-07 03:12:08 +0000 | [diff] [blame] | 641 | Be warned that you should calibrate the profiler class for the |
Guido van Rossum | df804f8 | 1995-03-02 12:38:39 +0000 | [diff] [blame] | 642 | timer function that you choose. For most machines, a timer that |
| 643 | returns a lone integer value will provide the best results in terms of |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 644 | low overhead during profiling. (\function{os.times()} is |
Tim Peters | 0a1fc4e | 2001-10-07 03:12:08 +0000 | [diff] [blame] | 645 | \emph{pretty} bad, as it returns a tuple of floating point values). If |
| 646 | you want to substitute a better timer in the cleanest fashion, |
| 647 | derive a class and hardwire a replacement dispatch method that best |
Fred Drake | 8fa5eb8 | 1998-02-27 05:23:37 +0000 | [diff] [blame] | 648 | handles your timer call, along with the appropriate calibration |
Fred Drake | 62f9d7c | 2001-06-08 05:04:19 +0000 | [diff] [blame] | 649 | constant. |