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