1- \chapter {The Python Profiler \label {profile } }
1+ \chapter {The Python Profilers \label {profile } }
22
33\sectionauthor {James Roskind}{}
44
55Copyright \copyright {} 1994, by InfoSeek Corporation, all rights reserved.
66\index {InfoSeek Corporation}
77
88Written 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.}
9+ Updated and converted to \LaTeX \ by Guido van Rossum.
10+ Further updated by Armin Rigo to integrate the documentation for the new
11+ \module {cProfile} module of Python 2.5.}
1112
1213Permission to use, copy, modify, and distribute this Python software
1314and its associated documentation for any purpose (subject to the
@@ -41,7 +42,7 @@ \chapter{The Python Profiler \label{profile}}
4142I'd appreciate the feedback.
4243
4344
44- \section {Introduction to the profiler }
45+ \section {Introduction to the profilers }
4546\nodename {Profiler Introduction}
4647
4748A \dfn {profiler} is a program that describes the run time performance
@@ -54,6 +55,31 @@ \section{Introduction to the profiler}
5455\index {deterministic profiling}
5556\index {profiling, deterministic}
5657
58+ The Python standard library provides three different profilers:
59+
60+ \begin {enumerate }
61+ \item \module {profile}, a pure Python module, described in the sequel.
62+ Copyright \copyright {} 1994, by InfoSeek Corporation.
63+ \versionchanged [also reports the time spent in calls to built-in
64+ functions and methods]{2.4}
65+
66+ \item \module {cProfile}, a module written in C, with a reasonable
67+ overhead that makes it suitable for profiling long-running programs.
68+ Based on \module {lsprof}, contributed by Brett Rosen and Ted Czotter.
69+ \versionadded {2.5}
70+
71+ \item \module {hotshot}, a C module focusing on minimizing the overhead
72+ while profiling, at the expense of long data post-processing times.
73+ \versionchanged [the results should be more meaningful than in the
74+ past: the timing core contained a critical bug]{2.5}
75+ \end {enumerate }
76+
77+ The \module {profile} and \module {cProfile} modules export the same
78+ interface, so they are mostly interchangeables; \module {cProfile} has a
79+ much lower overhead but is not so far as well-tested and might not be
80+ available on all systems. \module {cProfile} is really a compatibility
81+ layer on top of the internal \module {_lsprof} module. The
82+ \module {hotshot} module is reserved to specialized usages.
5783
5884% \section{How Is This Profiler Different From The Old Profiler?}
5985% \nodename{Profiler Changes}
@@ -108,10 +134,13 @@ \section{Instant Users Manual \label{profile-instant}}
108134you would add the following to your module:
109135
110136\begin {verbatim }
111- import profile
112- profile .run('foo()')
137+ import cProfile
138+ cProfile .run('foo()')
113139\end {verbatim }
114140
141+ (Use \module {profile} instead of \module {cProfile} if the latter is not
142+ available on your system.)
143+
115144The above action would cause \function {foo()} to be run, and a series of
116145informative lines (the profile) to be printed. The above approach is
117146most useful when working with the interpreter. If you would like to
@@ -120,21 +149,21 @@ \section{Instant Users Manual \label{profile-instant}}
120149function:
121150
122151\begin {verbatim }
123- import profile
124- profile .run('foo()', 'fooprof')
152+ import cProfile
153+ cProfile .run('foo()', 'fooprof')
125154\end {verbatim }
126155
127- The file \file {profile .py} can also be invoked as
156+ The file \file {cProfile .py} can also be invoked as
128157a script to profile another script. For example:
129158
130159\begin {verbatim }
131- python -m profile myscript.py
160+ python -m cProfile myscript.py
132161\end {verbatim }
133162
134- \file {profile .py} accepts two optional arguments on the command line:
163+ \file {cProfile .py} accepts two optional arguments on the command line:
135164
136165\begin {verbatim }
137- profile .py [-o output_file] [-s sort_order]
166+ cProfile .py [-o output_file] [-s sort_order]
138167\end {verbatim }
139168
140169\programopt {-s} only applies to standard output (\programopt {-o} is
@@ -153,7 +182,7 @@ \section{Instant Users Manual \label{profile-instant}}
153182The class \class {Stats} (the above code just created an instance of
154183this class) has a variety of methods for manipulating and printing the
155184data that was just read into \code {p}. When you ran
156- \function {profile .run()} above, what was printed was the result of three
185+ \function {cProfile .run()} above, what was printed was the result of three
157186method calls:
158187
159188\begin {verbatim }
@@ -162,8 +191,9 @@ \section{Instant Users Manual \label{profile-instant}}
162191
163192The first method removed the extraneous path from all the module
164193names. The second method sorted all the entries according to the
165- standard module/line/name string that is printed (this is to comply
166- with the semantics of the old profiler). The third method printed out
194+ standard module/line/name string that is printed.
195+ % (this is to comply with the semantics of the old profiler).
196+ The third method printed out
167197all the statistics. You might try the following sort calls:
168198
169199\begin {verbatim }
@@ -268,15 +298,17 @@ \section{What Is Deterministic Profiling?}
268298of algorithms to be directly compared to iterative implementations.
269299
270300
271- \section {Reference Manual }
301+ \section {Reference Manual -- \module {profile} and \module {cProfile} }
272302
273303\declaremodule {standard}{profile}
304+ \declaremodule {standard}{cProfile}
274305\modulesynopsis {Python profiler}
275306
276307
277308
278309The primary entry point for the profiler is the global function
279- \function {profile.run()}. It is typically used to create any profile
310+ \function {profile.run()} (resp. \function {cProfile.run()}).
311+ It is typically used to create any profile
280312information. The reports are formatted and printed using methods of
281313the class \class {pstats.Stats}. The following is a description of all
282314of these standard entry points and functions. For a more in-depth
@@ -296,7 +328,6 @@ \section{Reference Manual}
296328each line. The following is a typical output from such a call:
297329
298330\begin {verbatim }
299- main()
300331 2706 function calls (2004 primitive calls) in 4.504 CPU seconds
301332
302333Ordered by: standard name
@@ -307,9 +338,7 @@ \section{Reference Manual}
307338 ...
308339\end {verbatim }
309340
310- The first line indicates that this profile was generated by the call:\\
311- \code {profile.run('main()')}, and hence the exec'ed string is
312- \code {'main()'}. The second line indicates that 2706 calls were
341+ The first line indicates that 2706 calls were
313342monitored. Of those calls, 2004 were \dfn {primitive}. We define
314343\dfn {primitive} to mean that the call was not induced via recursion.
315344The next line: \code {Ordered by:\ standard name}, indicates that
@@ -350,7 +379,7 @@ \section{Reference Manual}
350379\end {funcdesc }
351380
352381\begin {funcdesc }{runctx}{command, globals, locals\optional {, filename}}
353- This function is similar to \function {profile. run()}, with added
382+ This function is similar to \function {run()}, with added
354383arguments to supply the globals and locals dictionaries for the
355384\var {command} string.
356385\end {funcdesc }
@@ -368,10 +397,12 @@ \section{Reference Manual}
368397manipulated by methods, in order to print useful reports.
369398
370399The file selected by the above constructor must have been created by
371- the corresponding version of \module {profile}. To be specific, there is
400+ the corresponding version of \module {profile} or \module {cProfile}.
401+ To be specific, there is
372402\emph {no } file compatibility guaranteed with future versions of this
373403profiler, and there is no compatibility with files produced by other
374- profilers (such as the old system profiler).
404+ profilers.
405+ % (such as the old system profiler).
375406
376407If several files are provided, all the statistics for identical
377408functions will be coalesced, so that an overall view of several
@@ -403,7 +434,8 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
403434This method of the \class {Stats} class accumulates additional
404435profiling information into the current profiling object. Its
405436arguments should refer to filenames created by the corresponding
406- version of \function {profile.run()}. Statistics for identically named
437+ version of \function {profile.run()} or \function {cProfile.run()}.
438+ Statistics for identically named
407439(re: file, line, name) functions are automatically accumulated into
408440single function statistics.
409441\end {methoddesc }
@@ -412,7 +444,8 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
412444Save the data loaded into the \class {Stats} object to a file named
413445\var {filename}. The file is created if it does not exist, and is
414446overwritten if it already exists. This is equivalent to the method of
415- the same name on the \class {profile.Profile} class.
447+ the same name on the \class {profile.Profile} and
448+ \class {cProfile.Profile} classes.
416449\versionadded {2.3}
417450\end {methoddesc }
418451
@@ -456,7 +489,8 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
456489compare of the line numbers. In fact, \code {sort_stats('nfl')} is the
457490same as \code {sort_stats('name', 'file' , 'line' )}.
458491
459- For compatibility with the old profiler, the numeric arguments
492+ % For compatibility with the old profiler,
493+ For backward-compatibility reasons, the numeric arguments
460494\code {-1}, \code {0}, \code {1}, and \code {2} are permitted. They are
461495interpreted as \code {'stdname'}, \code {'calls'}, \code {'time'}, and
462496\code {'cumulative'} respectively. If this old style format (numeric)
@@ -467,10 +501,10 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
467501
468502\begin {methoddesc }[Stats]{reverse_order}{}
469503This method for the \class {Stats} class reverses the ordering of the basic
470- list within the object. This method is provided primarily for
471- compatibility with the old profiler. Its utility is questionable
472- now that ascending vs descending order is properly selected based on
473- the sort key of choice.
504+ list within the object. % This method is provided primarily for
505+ % compatibility with the old profiler.
506+ Note that by default ascending vs descending order is properly selected
507+ based on the sort key of choice.
474508\end {methoddesc }
475509
476510\begin {methoddesc }[Stats]{print_stats}{\optional {restriction, \moreargs }}
@@ -512,10 +546,21 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
512546This method for the \class {Stats} class prints a list of all functions
513547that called each function in the profiled database. The ordering is
514548identical to that provided by \method {print_stats()}, and the definition
515- of the restricting argument is also identical. For convenience, a
516- number is shown in parentheses after each caller to show how many
517- times this specific call was made. A second non-parenthesized number
518- is the cumulative time spent in the function at the right.
549+ of the restricting argument is also identical. Each caller is reported on
550+ its own line. The format differs slightly depending on the profiler that
551+ produced the stats:
552+
553+ \begin {itemize }
554+ \item With \module {profile}, a number is shown in parentheses after each
555+ caller to show how many times this specific call was made. For
556+ convenience, a second non-parenthesized number repeats the cumulative
557+ time spent in the function at the right.
558+
559+ \item With \module {cProfile}, each caller is preceeded by three numbers:
560+ the number of times this specific call was made, and the total and
561+ cumulative times spent in the current function while it was invoked by
562+ this specific caller.
563+ \end {itemize }
519564\end {methoddesc }
520565
521566\begin {methoddesc }[Stats]{print_callees}{\optional {restriction, \moreargs }}
@@ -546,7 +591,10 @@ \section{Limitations \label{profile-limits}}
546591times, or call many functions, will typically accumulate this error.
547592The error that accumulates in this fashion is typically less than the
548593accuracy of the clock (less than one clock tick), but it
549- \emph {can } accumulate and become very significant. This profiler
594+ \emph {can } accumulate and become very significant.
595+
596+ The problem is more important with \module {profile} than with the
597+ lower-overhead \module {cProfile}. For this reason, \module {profile}
550598provides a means of calibrating itself for a given platform so that
551599this error can be probabilistically (on the average) removed.
552600After the profiler is calibrated, it will be more accurate (in a least
@@ -560,7 +608,7 @@ \section{Limitations \label{profile-limits}}
560608
561609\section {Calibration \label {profile-calibration } }
562610
563- The profiler subtracts a constant from each
611+ The profiler of the \module {profile} module subtracts a constant from each
564612event handling time to compensate for the overhead of calling the time
565613function, and socking away the results. By default, the constant is 0.
566614The following procedure can
@@ -614,11 +662,12 @@ \section{Calibration \label{profile-calibration}}
614662\section {Extensions --- Deriving Better Profilers }
615663\nodename {Profiler Extensions}
616664
617- The \class {Profile} class of module \module {profile} was written so that
665+ The \class {Profile} class of both modules, \module {profile} and
666+ \module {cProfile}, were written so that
618667derived classes could be developed to extend the profiler. The details
619668are not described here, as doing this successfully requires an expert
620669understanding of how the \class {Profile} class works internally. Study
621- the source code of module \ module{profile} carefully if you want to
670+ the source code of the module carefully if you want to
622671pursue this.
623672
624673If all you want to do is change how current time is determined (for
@@ -630,8 +679,11 @@ \section{Extensions --- Deriving Better Profilers}
630679pr = profile.Profile(your_time_func)
631680\end {verbatim }
632681
633- The resulting profiler will then call \code {your_time_func()}.
634- The function should return a single number, or a list of
682+ The resulting profiler will then call \function {your_time_func()}.
683+
684+ \begin {description }
685+ \item [\class {profile.Profile}]
686+ \function {your_time_func()} should return a single number, or a list of
635687numbers whose sum is the current time (like what \function {os.times()}
636688returns). If the function returns a single time number, or the list of
637689returned numbers has length 2, then you will get an especially fast
@@ -646,3 +698,22 @@ \section{Extensions --- Deriving Better Profilers}
646698derive a class and hardwire a replacement dispatch method that best
647699handles your timer call, along with the appropriate calibration
648700constant.
701+
702+ \item [\class {cProfile.Profile}]
703+ \function {your_time_func()} should return a single number. If it returns
704+ plain integers, you can also invoke the class constructor with a second
705+ argument specifying the real duration of one unit of time. For example,
706+ if \function {your_integer_time_func()} returns times measured in thousands
707+ of seconds, you would constuct the \class {Profile} instance as follows:
708+
709+ \begin {verbatim }
710+ pr = profile.Profile(your_integer_time_func, 0.001)
711+ \end {verbatim }
712+
713+ As the \module {cProfile.Profile} class cannot be calibrated, custom
714+ timer functions should be used with care and should be as fast as
715+ possible. For the best results with a custom timer, it might be
716+ necessary to hard-code it in the C source of the internal
717+ \module {_lsprof} module.
718+
719+ \end {description }
0 commit comments